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 ...
随机推荐
- Android——控件AutoCompleteTextView 自动提示
Android:控件AutoCompleteTextView 自动提示 在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextV ...
- C语言实现商品销售系统
商品销售系统 #include<stdio.h> //头文件 #include<string.h> //头文件 #include<stdlib.h> //头文件 # ...
- Game Loop的几种实现方式
http://www.bennychen.cn/2011/06/game-loop-model/ —————————————————————————————— 写这篇博客的目的是为了对game loo ...
- 创建ajax对象并兼容多个浏览器方法简单记录
这篇文章主要介绍了如何创建ajax对象并兼容多个浏览器,需要的朋友可以参考下<script> function createAjax(){ var request=false; //win ...
- 如何查询表A中的某字段的值在表B中不存在?
1.测试表创建,插入数据: create table a (id int, name )); create table b (id int); ,'a'); ,'b'); ,'c'); ,'d'); ...
- Git:如何为项目设置自己的user name/email
在项目根目录下查找 .git/config . 打开,添加如下内容(值换成你自己的名字和邮箱)即可: [user] name = Larry email = larry_zeal@163.com
- AOP(Aspect Oriented Programming),即面向切面编程
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- ThinkPHP的ajaxReturn方法的使用
ThinkPHP后端的代码如下: public function testAjax(){ $this->ajaxReturn(array('name'=>'z','age'=>18) ...
- 手淘H5移动端适配方案flexible源码分析
移动端适配一直是一个值得探讨的问题,在业余时间我找了一些页面,查看了一些厂商对于移动端H5页面的适配方案,看到了几个典型的例子,今天就来记录一下我看到的第一个典型的例子,也是我们公司目前普通H5项目正 ...
- linux vi编辑器中,如何通过快捷键上下翻页?
需求说明: 之前在vi的时候,如果想看下一页,就直接按住 ↓ 这个箭头一直翻,现在觉得有些麻烦, 就找了下上,下翻页的快捷方式.在此记录下. 记录: 1.向下翻页快捷键(下一页):Ctrl + f 2 ...