逆波兰表达式
1、栈
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String token : tokens) {
if(isNumber(token)){
stack.push(Integer.parseInt(token));
}else {
int number1=stack.pop();
int number2=stack.pop();
switch (token){
case "+":
stack.push(number1+number2);
break;
case "-":
stack.push(number2-number1);
break;
case "*":
stack.push(number1*number2);
break;
case "/":
stack.push(number2/number1);
break;
}
}
}
return stack.pop();
}
private boolean isNumber(String token) {
return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
}
}