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 .
The expression string contains only non-negative integers, +, -, *, / operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].
Note: Do not use the eval built-in library function.
Basic Idea:
和 Basic Calculator II 类似,只是需要将每个括号中的内容当做一个数来处理,调用自身处理括号内容。
Java Code:
classSolution{publicintcalculate(Strings){ s +="+";Deque<Integer> stack =newArrayDeque<>();char op ='+';int num =0;for(int i =0; i <s.length();++i){char c =s.charAt(i);if(c <='9'&& c >='0'){ num = num *10+ c -'0';}elseif(c =='('){ // 遇到 "(" 先处理括号中的内容int right =findRight(s, i); num =calculate(s.substring(i +1, right)); i = right;}elseif(c ==''){continue;}else{ // 遇到 op 的时候处理上一个 op,然后 op = cif(op =='+'|| op =='-'){stack.offerLast(op =='+'? num :-num);}elseif(op =='*'){stack.offerLast(stack.pollLast()* num);}elseif(op =='/'){stack.offerLast(stack.pollLast()/ num);} num =0; op = c;}}int ret =0;while(!stack.isEmpty()) ret +=stack.pollLast();return ret;}privateintfindRight(Strings,intleft){int count =0;while(left !=s.length()-1){char c =s.charAt(left);if(c =='(') count++;elseif(c ==')') count--;if(count ==0)return left; left++;}return-1;}}