Java Solution (Basic Calculator) 还没测,晚上回去册

class Count{
	 public int Count(String a){
		int  n = a.length();
		int result = 0;
		int  num = 0;
		int p = 0;
		int L1 = a.indexOf(‘(‘);
		int R1 = a.indexOf(‘)‘);
		String b = a.substring(L1+1,R1-1);
		int op = 0;
		if(L1 >= 0 ) return Count(b) + result;
		else
		{
			for(int i=0 ;i < n ;i++)
			{

				if(‘0‘<(a.charAt(i))&& (a.charAt(i))<‘9‘) num = num*10+num;
				switch(op)
				{
					case 0 : break;
					case 1 : num = num + p ;break;
					case 2 : num = p - num ;break;
					default : break;
				}
				if(a.charAt(i)==‘+‘)
				op = 1;
				else if (a.charAt(i)==‘-‘)
				op = 2 ;
				else
				op = 0;
				p = num ;

			}
			result = num;
			return result;

		}

			}
}这个鸟玩意用了迭代,其实不太好浪费空间,数据多了还容易死,有人用stack还没研究这个是600mm的还没看明白https://leetcode.com/discuss/39454/accepted-infix-postfix-based-solution-explaination-600ms

  

时间: 2024-10-02 18:35:03

Java Solution (Basic Calculator) 还没测,晚上回去册的相关文章

Java的CLASSPATH,趁还没忘赶紧写点

咳咳,睡眠不足加上年龄增长,真的赶脚记忆力不行啦. 接触Java以来,对于环境配置就是按照网上的教程,一路复制粘贴,也没啥想法; 最近决定啃啃ThinkInJava,没看两章就看到这CLASSPATH,一直以来都是用IDE从未遇到和它有关的时候; 网上找也都是它的配置教程,最后还是在wikipedia上看到了详细点的介绍; >_>描述也不好描述,还是用实例吧: 1.CLASSPATH环境变量配置一个目录(比如说C:\java); 2.把编译完的那些.class文件(通常为通用工具类,库之类的)

Leetcode 227. Basic Calculator II JAVA语言

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

Java for LeetCode 224 Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is

leetcode 227. Basic Calculator II ---------- java

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

Java程序员面试体会,还没找到工作的“猿猿们”看过来!

类似的话题,我大约在几个月前跟大家聊过.这一次,我要根据这段时间的体会,再给大家整理一下. 今天,很轻松地拿到了神州数码的offer.虽然工资不是特别高,虽然很多人一毕业就能进入这样的公司,但是对我这个半路出家的大专生来说,能进去纯属意外. 今天的面试,谈到了以前我接过一些私单,项目经理问我,那你现在还有没有做私活的想法?我说,彻底没有!他问我,为什么?我说,这几年,虽然做私活挣了一些钱,但我损失的远远不止那点钱.那些重复.繁琐,很难有技术水平提升的工作,占用了我大部份的业余时间,如果这些时间用

Leetcode solution 772: Basic Calculator III

Problem Statement Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . The expression string cont

Java for LeetCode 227 Basic Calculator II

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

Basic Calculator 基本计算器-Leetcode

1.题目: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expressi

Basic Calculator II Leetcode

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex