标签:包括 lis arrays exce exception 一个 商业 eof for
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
int len = tokens.length;
int ans = 0;
Integer op1, op2;
ArrayList<String> list =new ArrayList<>(Arrays.asList("+","-","*","/"));
for(int i = 0; i < len; i++){
if(list.contains(tokens[i])){
if(!stack.empty()){
op1 = stack.pop();
if(!stack.empty()){
op2 = stack.pop();
switch (tokens[i]){
case "+":
stack.push(op1+op2);
break;
case "-":
stack.push(op2-op1);
break;
case "*":
stack.push(op2*op1);
break;
case "/":
try{
stack.push(op2/op1);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}else{
stack.push(Integer.valueOf(tokens[i]));
}
}
return stack.pop();
}
}
标签:包括 lis arrays exce exception 一个 商业 eof for
原文地址:https://www.cnblogs.com/buaaZhhx/p/12378316.html