150. Evaluate Reverse Polish Notation
来源:互联网 发布:蝎子网络第一季百度云 编辑:程序博客网 时间:2024/05/22 08:24
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> mystack;for (int i = 0; i < tokens.size(); i++){string s = tokens[i];if (s.compare("+") == 0){int a = mystack.top();mystack.pop();int b = mystack.top();mystack.pop();mystack.push(a + b);continue;}if (s.compare("-") == 0){int a = mystack.top();mystack.pop();int b = mystack.top();mystack.pop();mystack.push(b - a);continue;}if (s.compare("*") == 0){int a = mystack.top();mystack.pop();int b = mystack.top();mystack.pop();mystack.push(a * b);continue;}if (s.compare("/") == 0){int a = mystack.top();mystack.pop();int b = mystack.top();mystack.pop();mystack.push(b / a);continue;}mystack.push(atoi(s.c_str()));}return mystack.top();}};
0 0
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 深入理解Java内存模型(二)——重排序
- Hadoop get JobId
- 二分查找的两种(找到val,找到第一val)
- yum报错Error: Cannot retrieve metalink for repository: epel
- 霍兰德职业测评
- 150. Evaluate Reverse Polish Notation
- JDBC(二)
- Android Launcher浅析(二)
- linux下c++使用pthread_create时需要调用类成员
- 获取图象匹配中的未匹配的特征点
- leetcode 27 Remove Element C++
- JavaScript学习--Item19 执行上下文(execution context)
- Fortify&Coverity
- 3rd ICMSSM 2016