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 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的更多相关文章
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- LeetCode OJ Basic Calculator II
Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...
- Leetcode solution 227: Basic Calculator II
Problem Statement Implement a basic calculator to evaluate a simple expression string. The expressio ...
随机推荐
- c# 获取系统时间
//获取日期+时间DateTime.Now.ToString(); // 2008-9-4 20:02:10DateTime.Now.ToLocalTime().ToStrin ...
- sql sever 字符串函数
SQL Server之字符串函数 以下所有例子均Studnet表为例: 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student ...
- ios如何普安短图片类型
很多时候需要知道服务器返回的图片是.png还是.jpg或者是.git, 两种方式 1,获取扩展名 //图片 NSString *image = @"4351141241.GIT&quo ...
- NOIP2005 等价表达式
题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数 ...
- 黄学长模拟day1 大逃亡
给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...
- ajax浅析---UpdatePanel
使用UpdatePanel控件 UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何 ...
- 第九天 iOS音频技术
1. AQRecorder mRecordFormat.mFormatID = inFormatID; if (inFormatID == kAudioFormatLinearPCM) { // if ...
- springmvc之log4j
1.工程结构 2.所需jar包 3.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- [lintcode 14] First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- 跟着百度学PHP[4]OOP面对对象编程-8-继承
如下图所示.人就是父类!而NBA球员以及女主播就是子类 要继承一个类,那么在类名的后面加上extends 要继承的类名 具体格式:class Student extends human{} # ...