150. Evaluate Reverse Polish Notation
2017-10-07 11:55
260 查看
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:
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
class Solution { public int evalRPN(String[] tokens) { Stack<Integer> s = new Stack<Integer>(); String operators = "+-*/"; for(String token : tokens){ if(!operators.contains(token)){ s.push(Integer.valueOf(token)); continue; } int a = s.pop(); int b = s.pop(); if(token.equals("+")) { s.push(b + a); } else if(token.equals("-")) { s.push(b - a); } else if(token.equals("*")) { s.push(b * a); } else { s.push(b / a); } } return s.pop(); } }
相关文章推荐
- 150. Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation
- LeetCode *** 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- [leetcode] 【栈】150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation(逆波兰式)
- 150. Evaluate Reverse Polish Notation (M)
- 150. Evaluate Reverse Polish Notation
- leetcode 150.Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- leetcode-150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- [LeetCode]150.Evaluate Reverse Polish Notation
- leecode 解题总结:150. Evaluate Reverse Polish Notation
- LeetCode-150. Evaluate Reverse Polish Notation (JAVA)逆波兰表达式求值
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- Middle-题目102:150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation