publicintcalculate(String s) {if (s ==null||s.isEmpty()) {return0; }int sign =1;int n =s.length();int num =0;int res =0;Stack<Integer> stack =newStack<>();for (int i =0; i < n; i++) {char cur =s.charAt(i);if (Character.isDigit(cur)) { num = num *10+Character.getNumericValue(cur); } elseif (cur =='+'|| cur =='-') { res += num * sign; num =0; sign = cur =='+'?1:-1; } elseif (cur =='(') {stack.push(res);stack.push(sign); res =0; sign =1; } elseif (cur ==')') { res += num * sign; sign =stack.pop(); num = res; res =stack.pop(); } } res += num * sign;return res;}