🟡 剑指 Offer II 036. 后缀表达式
LeetCode 提示
题目难度 中等
原题链接 🔗 leetcode
#
题解1class Solution { public int evalRPN(String[] tokens) { Deque<Integer> stack = new LinkedList<>(); int a, b;
for (String cur : tokens) { if (cur.equals("+")) { b = stack.pop(); a = stack.pop(); stack.push(a+b); } else if (cur.equals("-")) { b = stack.pop(); a = stack.pop(); stack.push(a-b); } else if (cur.equals("*")) { b = stack.pop(); a = stack.pop(); stack.push(a*b); } else if (cur.equals("/")) { b = stack.pop(); a = stack.pop(); stack.push((int)a/b); } else { stack.push(Integer.valueOf(cur)); } }
return stack.pop(); }}