LeetCode-Evaluate Reverse Polish Notation
2014-03-17 16:45
411 查看
水题一枚,直接压栈出栈即可,不过调用内置函数可以节省很多力气,如:
具体代码如下:
int stoi(const string& s);
具体代码如下:
class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> s; for(auto token:tokens){ if(is_digit(token)) s.push(stoi(token)); else{ int a = s.top(); s.pop(); int b = s.top(); s.pop(); switch(token[0]){ case '+': b+=a; break; case '-': b-=a; break; case '*': b*=a; break; case '/': b/=a; break; } s.push(b); } } return s.top(); } private: static bool is_digit(const string& token){ if((token.size()>1)||(token[0]>='0'&&token[0]<='9')) return true; return false; } };
相关文章推荐
- LeetCode: Evaluate Reverse Polish Notation
- leetcode Evaluate Reverse Polish Notation(*)
- LeetCode 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(计算逆波兰表达式) --java实现
- 【LeetCode】Evaluate Reverse Polish Notation(逆波兰表达式求值) -(Linkedin) Medium ++
- LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)
- [leetcode] Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- Leetcode 之Evaluate Reverse Polish Notation(41)
- [LeetCode]150 Evaluate Reverse Polish Notation
- LeetCode之EvaluateReversePolishNotation
- (leetcode)Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation (Medium) (cpp)
- leetcode之Evaluate Reverse Polish Notation
- 80_leetcode_Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation