Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, '+', '-', '*', '/' operators, and open '(' and closing parentheses ')'. 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 [-231, 231 - 1].
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
**Input:** s = "1+1"
**Output:** 2
Example 2:
**Input:** s = "6-4/2"
**Output:** 4
Example 3:
**Input:** s = "2*(5+5*2)/3+(6/2+8)"
**Output:** 21
Example 4:
**Input:** s = "(2+6*3+5-(3*14/7+2)*5)+3"
**Output:** -12
Example 5:
**Input:** s = "0"
**Output:** 0
Constraints:
1 <= s <= 104
s consists of digits, '+', '-', '*', '/', '(', and ')'.
s is a valid expression.
ac: Stack
classSolution {publicintcalculate(String s) {// Edge casesif (s ==null||s.length() ==0) return0;Deque<Character> operators =newArrayDeque<>();Deque<Integer> nums =newArrayDeque<>();int currNum =0;for (int i =0; i <s.length(); i++) {char c =s.charAt(i);if (c ==' ') continue;if (Character.isDigit(c)) { currNum = currNum *10+ (c -'0'); } elseif (c =='+'|| c =='-'|| c =='*'|| c =='/') {while (!operators.isEmpty() &&canDoPreviousOperation(operators.peek(), c)) { currNum =doOperation(nums.pop(),operators.pop(), currNum); }operators.push(c);nums.push(currNum); currNum =0; } elseif (c =='(') {operators.push('('); } elseif (c ==')') {while (operators.peek() !='(') { currNum =doOperation(nums.pop(),operators.pop(), currNum); }operators.pop(); // evict '(' } }while (!operators.isEmpty()) { currNum =doOperation(nums.pop(),operators.pop(), currNum); }return currNum; }privateintdoOperation(int num1,Character operator,int num2) {switch (operator) {case'+':return num1 + num2;case'-':return num1 - num2;case'*':return num1 * num2;case'/':return num1 / num2;default:thrownewIllegalArgumentException("Invalid operator."); } }privatebooleancanDoPreviousOperation(Character op1,Character op2) {if (op1 =='(') returnfalse;if (op2 =='+'|| op2 =='-') {returntrue; } else {// op2 is * or /if (op1 =='*'|| op1 =='/') returntrue; returnfalse; } }}/*E.g. ...(6 - 4 / 2)...num1, op1, currNum, {currOp}, {num3} ...1. currOp is +/-, or op1 && currOp both are * or /: currNum = eval(num1, op1, currNum)2. other cases: currNum = eval(currNum, currOp, num3). Because we don't know whether it is left parenthesis or num3 after currOp, let's push currNum, currOp to stack and do calculation later.
stack operators: [-]stack nums: [6]*/