LeetCode 150 Evaluate Reverse Polish Notation
2014-06-20 22:41
169 查看
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are
+,
-,
*,
/. Each operand may be an integer or another expression.Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public class Solution { public int evalRPN(String[] tokens) { int num1, num2, temp; Stack<Integer> sk = new Stack<Integer>(); for (String str : tokens) { if (str.equals("+")) { num1 = sk.pop(); num2 = sk.pop(); temp = num2 + num1; } else if (str.equals("-")) { num1 = sk.pop(); num2 = sk.pop(); temp = num2 - num1; } else if (str.equals("/")) { num1 = sk.pop(); num2 = sk.pop(); temp = num2 / num1; } else if (str.equals("*")) { num1 = sk.pop(); num2 = sk.pop(); temp = num2 * num1; } else temp = Integer.parseInt(str); sk.push(temp); } return sk.pop(); } }
相关文章推荐
- LeetCode150 Evaluate Reverse Polish Notation
- Leetcode 150:Evaluate Reverse Polish Notation
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
- LeetCode150:Evaluate Reverse Polish Notation
- Java for LeetCode 150 Evaluate Reverse Polish Notation
- [leetcode 150] Evaluate Reverse Polish Notation
- leetcode 150: Evaluate Reverse Polish Notation
- 【Leetcode-medium-150】Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- [Leetcode 150, Medium] Evaluate Reverse Polish Notation
- Leetcode 150:Evaluate Reverse Polish Notation(计算逆波兰表达式) --java实现
- leetcode-150-Evaluate Reverse Polish Notation
- [leetcode-150]Evaluate Reverse Polish Notation(c++)
- leetcode[150]Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- LeetCode150—Evaluate Reverse Polish Notation
- leetcode_c++:栈:Evaluate Reverse Polish Notation(150)
- LeetCode - 150 - Evaluate Reverse Polish Notation
- LeetCode 150 Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation