思路一:
逆波兰表达式其实就是后序遍历二叉树的序列
根据左右根的顺序入栈出栈即可
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer>stk1 = new Stack<Integer>();
Stack<Character>stk2 = new Stack<Character>();
for(int i=0;i<tokens.length;i++){
char cc = tokens[i].charAt(0);
if(cc=='-'){
if(tokens[i].length()==1){
int b = stk1.pop();
int a = stk1.pop();
stk1.push(a-b);
}else{
stk1.push(Integer.parseInt(tokens[i]));
}
}
if(cc=='+'){
int b = stk1.pop();
int a = stk1.pop();
stk1.push(a+b);
}
if(cc=='*'){
int b = stk1.pop();
int a = stk1.pop();
stk1.push(a*b);
}
if(cc=='/'){
int b = stk1.pop();
int a = stk1.pop();
stk1.push(a/b);
}
if(cc>='0'&&cc<='9'){
stk1.push(Integer.parseInt(tokens[i]));
}
}
return stk1.pop();
}
}