class Solution {
public static int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(String s:tokens){
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
int firstNum = stack.pop();
int secondNum = stack.pop();
stack.push(oprate(secondNum,firstNum,s));
}else {
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
private static int oprate(int x,int y,String op){
switch (op) {
case "+":
return x + y;
case "-":
return x - y;
case "*":
return x * y;
default:
return x / y;
}
}
}
leetcode 150. 逆波兰表达式求值
最新推荐文章于 2024-09-02 16:50:54 发布