一、题目内容
二、题解
解法1:
思路
使用栈,遇见数字入栈,遇见符号,弹出两个数字做运算,然后将结果入栈
代码
class Solution {public int evalRPN(String[] tokens) {Stack<Integer> stack = new Stack<Integer>();for(String token: tokens){if(isNumber(token)){stack.push(Integer.parseInt(token));}else{Integer num1 = stack.pop();Integer num2 = stack.pop();switch (token) {case "+":stack.push(num1 + num2);break;case "-":stack.push(num1 - num2);break;case "*":stack.push(num1 * num2);break;case "/":stack.push(num1 / num2);break;default:}}}return stack.pop();}public boolean isNumber(String token) {return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));}}
