###### [leetcode]Basic Calculator(java)

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 int calculate(String s) {
Stack<Character> charStack = new Stack<Character>();
Stack<Integer> intStack = new Stack<Integer>();

int index = -1;
int length = s.length();
char tmpChar;
int top1,top2;
while((++index)<length){
if((tmpChar=s.charAt(index)) == ' ')//ignore blank
continue;
switch (tmpChar) {
case '(':
charStack.push(tmpChar);
break;
case '-':
case '+':
if(charStack.isEmpty())
charStack.push(tmpChar);
else if(charStack.peek()=='(')
charStack.push(tmpChar);
else {
char op = charStack.pop();
charStack.push(tmpChar);
switch (op) {
case '+':
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top1+top2);
break;
default:
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top2-top1);
break;
}
}
break;
case ')':
while((tmpChar=charStack.pop())!='('){
switch (tmpChar) {
case '+':
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top1+top2);
break;
case '-':
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top2-top1);
break;
}
}
break;
default://number
//form intnumber,and push to intstack
int number = 0;
while(tmpChar>='0'&&tmpChar<='9'){
number = number*10+tmpChar-'0';
if(++index<length)
tmpChar = s.charAt(index);
else
break;
}
intStack.push(number);
index--;
break;
}
}
//process the last one
while(!charStack.isEmpty()){
tmpChar = charStack.pop();
switch (tmpChar) {
case '+':
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top2+top1);
break;//+
default:
top1 = intStack.pop();
top2 = intStack.pop();
intStack.push(top2-top1);
break;//-
}
}
return (intStack.isEmpty())?0:intStack.pop();
}

 public int calculate(String s){
int length = s.length();
Stack<Integer> signStack  = new Stack<Integer>();
signStack.push(1);//object
signStack.push(1);//firstElement sign

int index = -1;
char tmpChar;
int result = 0;

while(++index<length){
if((tmpChar=s.charAt(index))==' ')
continue;//ignore blank
switch (tmpChar) {
case '+':
case '(':
signStack.push(signStack.peek());
break;
case '-':
signStack.push(-1*(signStack.peek()));
break;
case ')':
signStack.pop();
break;
default://digital
int num=0;
while(tmpChar>='0'&&tmpChar<='9'){
num=num*10+tmpChar-'0';
if(++index<length)
tmpChar = s.charAt(index);
else
break;
}
result+=signStack.pop()*num;
index--;
break;
}
}
return result;
}

#### [Leetcode] 770. Basic Calculator IV 解题报告

2018-03-21 16:40:42

#### LeetCode 770. Basic Calculator IV题目解析

2018-01-26 12:47:08

#### 【LeetCode】Basic Calculator 解题报告

2015-06-14 11:19:17

#### Basic Calculator （非常重要）

2016-08-13 17:46:00

#### leetcode -- Basic Calculator I &II --重点，未完全理解

2015-12-16 12:33:48

#### 224. Basic Calculator | Java最短代码实现

2016-03-20 15:12:17

#### LeetCode224 BasicCalculator java题解

2015-07-02 21:52:24

#### Basic_Calculator

2017-10-09 16:58:12

#### Basic Calculator

2015-09-27 20:58:11

#### Leetcode 224: Basic Calculator

2015-06-19 00:59:08

## 不良信息举报

[leetcode]Basic Calculator(java)