import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
public int solve (String s) {
// write code here
Map<Character,Integer> map = new HashMap<>();
map.put('+',1);
map.put('-',1);
map.put('*',2);
Deque<Integer> nums = new ArrayDeque<>();
Deque<Character> opts = new ArrayDeque<>();
s.replaceAll(" ","");
char c1[] = s.toCharArray();
int n = s.length();
for(int i=0;i<n;i++){
char c = c1[i];
if(isnumber(c)){
int num=0;
int j=i;
while(j<n && isnumber(c1[j]))
num = num*10+(c1[j++]-'0');
nums.push(num);
i=j-1;
}
else if(c=='(')
opts.push(c);
else if(c==')'){
while(!opts.isEmpty()&&opts.peek()!='(')
cal(nums,opts);
opts.pop();
}
else{
while(!opts.isEmpty()&& opts.peek()!='(' && map.get(c)<=map.get(opts.peek())){
cal(nums,opts);
}
opts.push(c);
}
}
while(!opts.isEmpty()){
cal(nums,opts);
}
return nums.pop();
}
public static boolean isnumber(Character c){
return Character.isDigit(c);
}
public static void cal(Deque<Integer> nums, Deque<Character> opts){
int num2 = nums.pop();
int num1 = nums.pop();
char opt = opts.pop();
if(opt=='+')
nums.push(num1+num2);
else if(opt=='-')
nums.push(num1-num2);
else
nums.push(num1*num2);
}
}