[LeetCode]150. Evaluate Reverse Polish Notation
2017-03-20 21:21
507 查看
150. Evaluate Reverse Polish Notation
题目描述
思路
遇到数字压栈,遇到运算符出栈运算,结果压栈整体思路比较简单,看讨论区有设计到C++的函数式编程,留在这里记录一下
代码
class Solution { public: int evalRPN(vector<string>& tokens) { unordered_map<string, function<int(int, int) > > map = { { "+" , [](int a, int b) { return a + b; } }, { "-" , [](int a, int b) { return a - b; } }, { "*" , [](int a, int b) { return a * b; } }, { "/" , [](int a, int b) { return a / b; } } }; std::stack<int> stack; for (string& s : tokens) { if (!map.count(s)) { stack.push(stoi(s)); } else { int op1 = stack.top(); stack.pop(); int op2 = stack.top(); stack.pop(); stack.push(map[s](op2, op1)); } } return stack.top(); } };
相关文章推荐
- LeetCode--150. Evaluate Reverse Polish Notation
- [leetcode] 150.Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- [LeetCode]150.Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation(计算后缀表达式)
- leetcode题解-150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- LeetCode-150. Evaluate Reverse Polish Notation (JAVA)逆波兰表达式求值
- leetcode 150. Evaluate Reverse Polish Notation
- [leetcode] 150. Evaluate Reverse Polish Notation
- [leetcode] 【栈】150. Evaluate Reverse Polish Notation
- Leetcode刷题记——150. Evaluate Reverse Polish Notation(计算逆波兰表达式)
- leetcode 150.Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation (Medium) (cpp)
- Leetcode 150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation