LeetCode题解——Evaluate Reverse Polish Notation
来源:互联网 发布:爱知学院大学 编辑:程序博客网 时间:2024/06/09 14:51
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> s; int v1,v2; for(auto c:tokens){ if(c=="*"){ v1 = s.top(); s.pop(); v2 = s.top(); s.pop(); s.push(v1*v2); } else if(c=="/"){ v1 = s.top(); s.pop(); v2 = s.top(); s.pop(); s.push(int(v2/v1)); } else if(c=="+"){ v1 = s.top(); s.pop(); v2 = s.top(); s.pop(); s.push(v1+v2); } else if(c=="-"){ v1 = s.top(); s.pop(); v2 = s.top(); s.pop(); s.push(v2-v1); } else s.push(stoi(c)); } return s.top(); }};
0 0
- 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
- leetcode 150 —— Evaluate Reverse Polish Notation
- LeetCode—Evaluate Reverse Polish Notation解题报告
- LeetCode 之 Evaluate Reverse Polish Notation — C++ 实现
- Evaluate Reverse Polish Notation | leetcode
- 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
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- IOS开发系列--无限循环的图片浏览器
- 《大话数据结构》之Kruskal算法
- javaWeb 复习系列(二)
- sublime 快捷键大全
- Response.Flush和Response.BufferOutput
- LeetCode题解——Evaluate Reverse Polish Notation
- 安卓中轻量级数据库SQLite的使用
- js获取子元素
- Linux下iptables 禁止端口和开放端口(仅供参考,里面含有iptables如何指定允许外网访问的端口号范围,以及对局域网的ip全部开放端口)
- 抽象类和接口的应用
- 自定义UIbutton的样式
- 第一篇博客
- 报表设计中参数、脚本的使用以及生成系统时间
- eclipse导入工程失败解决方法