目录
题目链接
链接: 逆波兰表达式求值
题目要求


解题思路


代码实现
java
class Solution {
private static boolean isOperations(String ch){
if(ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/")){
return true;
}
return false;
}
public static int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < tokens.length; i++) {
if(!isOperations(tokens[i])){
//字符数字转数字
stack.push(Integer.valueOf(tokens[i]));
}else{
int a = stack.pop();
int b = stack.pop();
switch(tokens[i]){
case "+":
stack.push(b + a);
break;
case "-":
stack.push(b - a);
break;
case "*":
stack.push(b * a);
break;
case "/":
stack.push(b / a);
break;
}
}
}
return stack.pop();
}
}
