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. SQL Server 2012大幅增强T-SQL

    SQL Server 2012对T-SQL进行了大幅增强,其中包括支持ANSI FIRST_VALUE和LAST_VALUE函数,支持使用FETCH与OFFSET进行声明式数据分页,以及支持.NET中 ...

  2. GridView_RowCommand事件中取得GridViewRow

    GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow;

  3. 【Leafletjs】1.创建一个地图

    code: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <l ...

  4. json对象转为字符串,当做参数传递时加密解密

    [son对象  字符串 互相转行] 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":"tom","sex ...

  5. Redis有序集合Zset(sorted set)

    zadd/zrange 127.0.0.1:6379> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5(integer) 5127.0.0.1:6379&g ...

  6. can not import Flask错误

    在学习flask官方文档——应用构建代码时,遇到了一个小问题照例子敲好代码以后出现了can not import  Flask错误,思考很久也没有解决,最后发现是因为应用模块和flask重名了.下面是 ...

  7. 边工作边刷题:70天一遍leetcode: day 75

    Group Shifted Strings 要点:开始就想到了string之间前后字符diff要相同. 思维混乱的地方:和某个string的diff之间是没有关系的.所以和单个string是否在那个点 ...

  8. 【转载】为什么CPU有多层缓存

    原文:为什么CPU有多层缓存 http://mp.weixin.qq.com/s?__biz=MzI1NDM2Nzg5Mw==&mid=2247483712&idx=1&sn= ...

  9. CentOS下编译安装MySQL 5.6.21

    一.编译安装MySQL前的准备工作 安装编译源码所需的工具和库 yum install gcc gcc-c++ ncurses-devel perl 安装cmake:http://www.cnblog ...

  10. 带你快速了解CODESOFT 2015

    CODESOFT是知名的条码标签设计打印软件,现在又推出了最新版CODESOFT 2015,其新功能.大改进让人为之一振.下面跟随小编的脚步,走进CODESOFT 2015,看一看CODESOFT 2 ...