LeetCode——Basic Calculator
Description:
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 always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval
built-in library function.
用两个操作栈来处理,具体见代码。
public class Solution {
public static int calculate(String s) {
Stack<Integer> num = new Stack<>();
Stack<Character> op = new Stack<>();
op.push('+');
int n = s.length();
if(n < 1) return 0;
for(int i=0; i<n; ) {
if(s.charAt(i) == ' ') {
i ++;
}
else if(s.charAt(i) == '(') {
op.push('(');
op.push('+');
i ++;
}
else if(isOp(s.charAt(i))) {
op.push(s.charAt(i));
i ++;
}
//523 +-(++
else if(s.charAt(i) == ')') {
int res = 0;
while (!op.empty() && !(op.peek() == '(')) {
int temp = num.peek();
if (op.peek() == '-') temp = -temp;
res += temp;
num.pop();
op.pop();
}
System.out.println(res);
op.pop();
num.push(res);
i ++;
}
else {
int temp = 0;
while(i < n && isDigit(s.charAt(i))) {
temp = temp * 10;
temp += s.charAt(i) - '0';
i ++;
} num.push(temp);
} }
int res = 0;
while (!op.empty()) {
int temp = num.peek();
if (op.peek() == '-') temp = -temp;
res += temp;
num.pop();
op.pop();
} return res; }
public static boolean isOp(char c) {
if(c == '+' || c == '-') {
return true;
}
else {
return false;
}
}
public static boolean isDigit(char c) {
if(c >= '0' && c <='9') {
return true;
}
else {
return false;
}
}
}
LeetCode——Basic Calculator的更多相关文章
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...
- LeetCode Basic Calculator
原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- LeetCode——Basic Calculator II
Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...
- LeetCode() Basic Calculator 不知道哪里错了
class Solution {public: int calculate(string s) { stack<int> num; stack<ch ...
随机推荐
- TP5 display()
tp3.x $this->display(); tp5 return $this->fetch();
- [转]pageX、clientX、screenX、offsetX、layerX、x
参考:http://www.cnblogs.com/xesam/archive/2011/12/08/2280509.html chrome: e.pageX——相对整个页面的坐标e.layerX—— ...
- DevExpress gridcontrol学习知识点
.//允许自动合并单元格gv1.OptionsView.AllowCellMerge = true; 设置某列不合并 选中该列,在OptionsColumns-->AllowMerge中设置为f ...
- java- 综合实例-增删查改查,删除多项,分页,令牌机制
重点内容:分页.令牌机制(重定向下防止重复提交).使用c3p0连接数据库(以及数据库连接类) 项目结构: 类: 项目展示: 数据库: /* SQLyog Ultimate v12.09 (64 bit ...
- 腾讯RTX二次开发相关的一些注意事项
http://www.cnblogs.com/netWild/p/4241650.html —————————————————————————————————————————————————————— ...
- Javascript 你不知道的事,好吧,是我不知道的事
NaN表示一个不能产生正常结果的运算结果.它不等于任何值,包括它自己.可以用isNaN(number)来检测. 同Java中的字符串一样,JS中的字符串是不可变的.也就是说一旦字符串被创建,就无法改变 ...
- Spark 快速理解
转自:http://blog.csdn.net/colorant/article/details/8255958 ==是什么 == 目标Scope(解决什么问题) 在大规模的特定数据集上的迭代运算或重 ...
- (转)SDL 1.2 to 2.0 Migration Guide--SDL1.2更新到SDL2.0指南
SDL 1.2 to 2.0 Migration Guide 目录 SDL 1.2 to 2.0 Migration Guide Translations Introduction Overview ...
- Emojis support in Apple push notification
I am working on iPhone app named "INTERSTIZIO".In this I have implemented functionality li ...
- c++ ++i and i++
++i 在 i 存储的值上增加一并向使用它的表达式 ``返回" 新的, 增加后的值; 而 i++ 对 i 增加一, 但返回原来的是未增加的值. 至于++i和i++有什么区别,举个例子 1.a ...