public int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
int result = 0;
int number = 0;
int sign = 1;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
number = 10 * number + (int)(c - '0');
}else if(c == '+'){
result += sign * number;
number = 0;
sign = 1;
}else if(c == '-'){
result += sign * number;
number = 0;
sign = -1;
}else if(c == '('){
//we push the result first, then sign;
stack.push(result);
stack.push(sign);
//reset the sign and result for the value in the parenthesis
sign = 1;
result = 0;
}else if(c == ')'){
result += sign * number;
number = 0;
result *= stack.pop(); //stack.pop() is the sign before the parenthesis
result += stack.pop(); //stack.pop() now is the result calculated before the parenthesis
}
}
if(number != 0) result += sign * number;
return result;
}
public class Solution {
public int calculate(String s) {
//valueStack and signStack
//when meet sign, if next is "(" push res and sign into stacks, if next is integer, sign * integer plus res
s = s.trim();
if(s.length() == 0) return 0;
Stack<Integer> valueStack = new Stack<>();
Stack<Integer> signStack = new Stack<>();
char[] chars = s.toCharArray();
int i = 0, res = 0, sign = 1;
while(i < chars.length) {
//System.out.println(chars[i]);
if(chars[i] == '+') {
sign = 1;
}
else if(chars[i] == '-') {
sign = -1;
}
else if(chars[i] >= '0' && chars[i] <= '9') {
int j = i;
int cur = 0;
while(j < chars.length && chars[j] >= '0' && chars[j] <= '9') {
cur = cur * 10 + chars[j] - '0';
j++;
}
i = --j;
//System.out.println(cur);
res += sign * cur;
}
else if(chars[i] == '(') {
valueStack.push(res);
signStack.push(sign);
res = 0;
sign = 1;
}
else if(chars[i] == ')') {
res = res * signStack.pop() + valueStack.pop();
}
else if(chars[i] == ' ') {
i++;
continue;
}
i++;
}
return res;
}
}
public class Solution {
private int getResult(int a, int b, char op) {
switch(op) {
case '+' : return a + b;
default : return a - b;
}
}
public int calculate(String s) {
if (s == null || s.length() < 1) return 0;
Stack<Character> operators = new Stack<>();
Stack<Integer> operands = new Stack<>();
char[] str = s.toCharArray();
int len = str.length;
int num = 0;
int i = 0;
while (i < len) {
if (str[i] == ' ') {
i++;
continue;
}
if (str[i] >= '0' && str[i] <= '9') {
num = str[i] - '0';
i++;
while (i < len && str[i] >= '0' && str[i] <= '9') {
num = num * 10 + str[i] - '0';
i++;
}
operands.push(num);
num = 0;
} else if (str[i] == '(') {
operators.push(str[i++]);
} else if (str[i] == ')') {
while (operators.peek() != '(') {
int second = operands.pop();
int first = operands.pop();
operands.push(getResult(first, second, operators.pop()));
}
operators.pop();
i++;
} else if (str[i] == '+' || str[i] == '-') {
while (!operators.isEmpty() && operators.peek() != '(') {
int second = operands.pop();
int first = operands.pop();
operands.push(getResult(first, second, operators.pop()));
}
operators.push(str[i++]);
}
}
while (!operators.isEmpty()) {
int second = operands.pop();
int first = operands.pop();
operands.push(getResult(first, second, operators.pop()));
}
return operands.pop();
}
}
public class Solution {
public int calculate(String s) {
if (s == null || s.length() == 0) return 0;
Stack<Integer> stack = new Stack<>();
int res = 0;//curr result
int sign = 1;//sign: the previous sign for curr num. initialize it as 1 is important!
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int sum = c - '0';//sum up the whole num
while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))) {//s.charAt(i + 1), not i + 1 !!!
sum = sum * 10 + s.charAt(i + 1) - '0';//see how to get the curr num
i++;
}
res += sum * sign;//add the sum to res
} else if (c == '+') {
sign = 1;
} else if (c == '-') {
sign = -1;
} else if (c == '(') {
stack.push(res);
stack.push(sign);//sign is on the top
res = 0;//update res to 0, which is the sum in the ( )
sign = 1;
} else if (c == ')') {
res = res * stack.pop() + stack.pop();//first pop is the sign, second is the previous sum before ( )
}
}
return res;
}
}