leetcode - Evaluate Reverse Polish Notation
2013-12-07 12:30
591 查看
class Solution { public: void parseNum(stack<int>& _stack, const string & token){ int number = atoi(token.c_str()); _stack.push(number); } void parseOperation(stack<int> & _stack, char token){ int num1 = _stack.top(); _stack.pop(); int num2 = _stack.top(); _stack.pop(); int rlt = 0; switch(token){ case '+': rlt= num2+num1; break; case '-': rlt = num2-num1; break; case '*': rlt= num2*num1; break; case '/': rlt = num2/num1; break; } _stack.push(rlt); } int evalRPN(vector<string> &tokens) { stack<int> _stack; vector<string>::iterator itr;; for (itr=tokens.begin(); itr!=tokens.end(); itr++){ string token = * itr; if (token.length()==1){ char c = token.at(0); if (c=='+' || c=='-' || c=='*' || c=='/') parseOperation(_stack, c); else parseNum(_stack, token); } else parseNum(_stack, token); } if (!_stack.empty()) return _stack.top(); return 0; } };
相关文章推荐
- 【LeetCode】150 Evaluate Reverse Polish Notation 小侃小解
- LeetCode—Evaluate Reverse Polish Notation 逆波兰写法的解析
- leetcode - Evaluate Reverse Polish Notation
- leetcode_2 Evaluate Reverse Polish Notation 逆波兰表达式
- 【LeetCode-面试算法经典-Java实现】【150-Evaluate Reverse Polish Notation(计算逆波兰式)】
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode_Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
- leetcode[150]Evaluate Reverse Polish Notation
- [LeetCode]Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- [LeetCode]Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation--LeetCode
- evaluate-reverse-polish-notation(Leetcode)
- [LeetCode] Evaluate Reverse Polish Notation
- 【LeetCode练习题】Evaluate Reverse Polish Notation
- 每日AC - 后缀表达式计算结果-leetcode-evaluate-reverse-polish-notation
- LeetCode_Stack_Evaluate Reverse Polish Notation
- LeetCode题解:Evaluate Reverse Polish Notation