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 expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
解题思路:

注意:测试中会出现“4*3/5”这样的情况,因此,可以用一个TempRes保存上一次运算的结果,TempOp表示距离本次运算符最近的+-运算符,具体看代码即可:

	static public int calculate(String s) {
		int res = 0, tempRes = 0, Option = 1, tempOp = 1;
		for (int i = 0; i < s.length(); i++) {
			switch (s.charAt(i)) {
			case ' ':
				break;
			case '+':
				Option = 1;
				break;
			case '-':
				Option = -1;
				break;
			case '*':
				Option = 2;
				break;
			case '/':
				Option = 3;
				break;
			default:
				int num = 0;
				while (i < s.length() && Character.isDigit(s.charAt(i)))
					num = num * 10 + s.charAt(i++) - '0';
				i--;
				switch (Option) {
				case 1:
					res += tempOp * tempRes;
					tempRes = num;
					tempOp = 1;
					break;
				case -1:
					res += tempOp * tempRes;
					tempRes = num;
					tempOp = -1;
					break;
				case 2:
					tempRes *= num;
					break;
				case 3:
					tempRes /= num;
				}

			}
		}
		res += tempOp * tempRes;
		return res;
	}

Java for LeetCode 227 Basic Calculator II的更多相关文章

  1. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  2. (medium)LeetCode 227.Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. Java for LeetCode 224 Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  4. 227. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Basic Calculator,Basic Calculator II

    一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...

  6. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

  7. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  8. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. Hibernnate延迟加载策略(这么详细你还看不懂)

    好久没有认真写过博客了,今天就好好的写一篇吧!!!!!!!!! 当Hibernate 从数据库中加载某个对象(例如:Dept对象)时,如果同时自动加载所有的关联的某个对象(例如:Emp对象),而程序实 ...

  2. SVN提交代码的规范

       协同开发中SVN的使用规范 先更新,再提交 SVN更新的原则是要随时更新,随时提交.当完成了一个小功能,能够通过编译并且自己测试之后,谨慎地提交. 如果在修改的期间别人也更改了svn的对应文件, ...

  3. 尚硅谷-Maven笔记

  4. a标签的target指向iframe

    <html> <head> <meta charset="utf-8" /> </head> <body> <ta ...

  5. Retrieve失败解决办法一例

    错误:The service '/XRMServices/2011/OrganizationData.svc' cannot be activated due to an exception duri ...

  6. (转)C语言union(联合体 共用体)

    一直以来,union都是个很少用到的东西,对于这些不常用的结构往往记不住.这次看书又看到了,还是学习一下吧.一般在Windows API的一些数据结构中才能看到这个union,其实并不复杂.本质上来说 ...

  7. bootstrap dialog自行控制窗口的关闭

    在使用dialog的时候,我们通常不希望点击btn的时候自动隐藏dialog,通常需要做一些清理或者ajax操作,在bootstrap dialog中,这是通过 data-dismiss=" ...

  8. 【HDU 4150】Powerful Incantation

    题 题意 给你s1,s2两个字符串,求s1中有多少个s2 代码 #include<stdio.h> #include<string.h> int t,len1,len2,pos ...

  9. leetcode:Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  10. Transaction Log Truncation

    --method 1-- ALTER DATABASE KIS_Sample3 SET RECOVERY SIMPLE ) ALTER DATABASE KIS_Sample3 SET RECOVER ...