150. Evaluate Reverse Polish Notation
来源:互联网 发布:异常断电数据库 编辑:程序博客网 时间:2024/05/21 22:54
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
Subscribe to see which companies asked this question
class Solution {public:int evalRPN(vector<string>& tokens) {stack<int> stk;for (string str : tokens){if (!isOp(str)){stk.push(stoi(str));}else{int num1 = stk.top(); stk.pop();int num2 = stk.top(); stk.pop();int num=0;switch (str[0]){case '+':num = num1 + num2; break;case '-':num = num2 - num1; break;case '*':num = num1*num2; break;case '/':num = num2 / num1; break;default:break;}stk.push(num);}}return stk.top();}private:bool isOp(string str){if (str.size()==1&&(str[0] == '+' || str[0] == '-' ||str[0] == '*' || str[0] == '/')){return true;}return false;}};
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
- Ubuntu建立wifi热点(支持Android手机)
- read函数和fread函数的区别
- python学习(10)————函数与模块
- iOS6与iOS7屏幕适配技巧
- lucene6.1.0 for linux 学习一:配置
- 150. Evaluate Reverse Polish Notation
- 随手敲代码——IOC猜想(终极版)
- Flume-ng集群安装文档
- MySQL数据库记录操作
- Matrix
- 天才的主意
- 几个概念
- java 枚举类型
- 网站跳转的几种实现方法