题目:
根据逆波兰表示法,求表达式的值。
有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> s=new Stack();
for(String str:tokens){
if(str.equals("+")){
int a=s.pop();
int b=s.pop();
s.push(a+b);
}else if(str.equals("-")){
int a=s.pop();
int b=s.pop();
s.push(b-a);
}else if(str.equals("*")){
int a=s.pop();
int b=s.pop();
s.push(b*a);
}else if(str.equals("/")){
int a=s.pop();
int b=s.pop();
s.push(b/a);
}else{
s.push(Integer.parseInt(str));
}
}
return s.pop();
}
}