public class Solution { public static int evalRPN(String[] tokens) { int num1, num2, res = 0; Stack<String> stack = new Stack<String>(); for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("+")) { num1 = Integer.parseInt(stack.pop()); num2 = Integer.parseInt(stack.pop()); res = num1 + num2; stack.push(String.valueOf(res)); } else if (tokens[i].equals("-")) { num1 = Integer.parseInt(stack.pop()); num2 = Integer.parseInt(stack.pop()); res = num2 - num1; stack.push(String.valueOf(res)); } else if (tokens[i].equals("*")) { num1 = Integer.parseInt(stack.pop()); num2 = Integer.parseInt(stack.pop()); res = num1 * num2; stack.push(String.valueOf(res)); } else if (tokens[i].equals("/")) { num1 = Integer.parseInt(stack.pop()); num2 = Integer.parseInt(stack.pop()); res = num2 / num1; stack.push(String.valueOf(res)); } else { stack.push(tokens[i]); } } return Integer.parseInt(stack.pop()); } }
[LeetCode]Evaluate Reverse Polish Notation, 解题报告,布布扣,bubuko.com
[LeetCode]Evaluate Reverse Polish Notation, 解题报告
原文:http://blog.csdn.net/wzy_1988/article/details/23999583