[leetcode]Evaluate Reverse Polish Notation
2014-05-14 19:29
176 查看
Evaluate Reverse Polish Notation
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
class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> si; int vslen = tokens.size(); int a, b; for(int i = 0; i < vslen; i++){ if(tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/"){ si.push(atoi(tokens[i].c_str())); continue; } b = si.top(); si.pop(); a = si.top(); si.pop(); if(tokens[i] == "+"){ si.push(a + b); }else if(tokens[i] == "-"){ si.push(a - b); }else if(tokens[i] == "*"){ si.push(a * b); }else if(tokens[i] == "/"){ //默认所有输入是有效的,不包含0 si.push(a / b); } } return si.top(); } };
相关文章推荐
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- [leetcode]Evaluate Reverse Polish Notation @ Python
- 【leetcode】Evaluate Reverse Polish Notation
- [LeetCode 第4题] -- Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation(java)
- 【leetcode】Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation--LeetCode
- LeetCode 2 Evaluate Reverse Polish Notation
- leetcode之Evaluate Reverse Polish Notation
- LeetCode – Refresh – Evaluate Reverse Polish Notation
- 【LeetCode】Evaluate Reverse Polish Notation(逆波兰表达式求值) -(Linkedin) Medium ++
- [LeetCode]Evaluate Reverse Polish Notation
- 【LeetCode】150. Evaluate Reverse Polish Notation
- 【LeetCode】Evaluate Reverse Polish Notation
- 【LeetCode】之Evaluate Reverse Polish Notation
- leetcode_c++:栈:Evaluate Reverse Polish Notation(150)
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
- 【LeetCode】-Evaluate Reverse Polish Notation