Evaluate Reverse Polish Notation
2015-02-01 15:12
106 查看
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
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(vector<string> &tokens) { int sum; string operators="+-*/"; stack<int>sta; for(int i=0;i<tokens.size();i++){ if(operators.find(tokens[i])==string::npos){ sta.push(atoi(tokens[i].c_str())); } else{ int a=sta.top(); sta.pop(); int b=sta.top(); sta.pop(); if(tokens[i]=="+"){ sta.push(a+b); } if(tokens[i]=="-"){ sta.push(b-a); } if(tokens[i]=="*"){ sta.push(a*b); } if(tokens[i]=="/"){ sta.push(b/a); } } } sum=sta.top(); return sum; } };
相关文章推荐
- LeetCode150—Evaluate Reverse Polish Notation
- Leetcode刷题记——150. Evaluate Reverse Polish Notation(计算逆波兰表达式)
- leetcode-Evaluate Reverse Polish Notation
- Java实现逆波兰表达式(Evaluate Reverse Polish Notation)
- [LeetCode] Evaluate Reverse Polish Notation, Solution
- [leetcode]Evaluate Reverse Polish Notation
- LeetCode150:Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- 【LeetCode】150. Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- leetcode之Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation (Python)
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation(LeetCode)
- LeetCode: Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation