leetcode 150. Evaluate Reverse Polish Notation
来源:互联网 发布:网络言论自由的事例 编辑:程序博客网 时间:2024/06/14 10:35
150. Evaluate Reverse Polish Notation
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计算后缀表达式!用STACK
class Solution {public: int evalRPN(vector<string>& tokens) { stack<int> shuzi; for (int i = 0; i < tokens.size(); i++) { if(tokens[i] == "+") { int a = shuzi.top(); shuzi.pop(); int b = shuzi.top(); shuzi.top() = a + b; } else if(tokens[i] == "-") { int a = shuzi.top(); shuzi.pop(); int b = shuzi.top(); shuzi.top() = b - a; } else if(tokens[i] == "*") { int a = shuzi.top(); shuzi.pop(); int b = shuzi.top(); shuzi.top() = a * b; } else if(tokens[i] == "/") { int a = shuzi.top(); shuzi.pop(); int b = shuzi.top(); shuzi.top() = b / a; } else shuzi.push( atoi(tokens[i].c_str()) ); } return shuzi.top(); }};
阅读全文
0 0
- 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
- 150. Evaluate Reverse Polish Notation LeetCode
- 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
- [leetcode] 150. Evaluate Reverse Polish Notation
- [leetcode] 【栈】150. Evaluate Reverse Polish Notation
- LeetCode - 150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- 超简单的居中
- 运行iDT算法代码及后续特征编码
- 类加载器
- 一天入门redis-安装连接、基本数据类型
- hbase基于快照的数据迁移
- leetcode 150. Evaluate Reverse Polish Notation
- 安装openssl和openssl-devel
- Apache kylin安装配置
- 5-1 继承与派生
- webservice调用没有按钮
- SylixOS设备分辨率设置
- IDEA破解 2017 IDEA license server 激活(可用)
- Python异常类
- 架构设计:系统存储(20)——图片服务器:需求和技术选型(2)