150.逆波兰表达式求值
方法:使用栈
class Solution {public int evalRPN(String[] tokens) {Stack<Integer> numStack = new Stack<>();for(int i = 0; i < tokens.length;i++){String token = tokens[i];if(isNumber(token)){numStack.push(Integer.parseInt(token));}else{int num1 = numStack.pop();int num2 = numStack.pop();switch(token){case "+":numStack.push(num1 + num2);break;case "-":numStack.push(num2 - num1);break;case "*":numStack.push(num1 * num2);break;case "/":numStack.push(num2 / num1);break;default:}}}return numStack.pop();}public boolean isNumber(String token){return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));}
}