[leetcode] 150. Evaluate Reverse Polish Notation 解题报告
来源:互联网 发布:图标大全软件 编辑:程序博客网 时间:2024/06/05 22:35
题目链接:https://leetcode.com/problems/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
思路:这是将运算符后置的方式,当然可以想到用栈来做,如果是数字就入栈,如果是运算符就取出栈顶的两个元素,运算之后将结果再压栈,最后栈顶元素就是最终的结果。
代码如下:
class Solution {public: int evalRPN(vector<string>& tokens) { stack<int> st; for(auto str:tokens) { if(str == "+" || str == "-" || str == "*" || str == "/") { int b = st.top(); st.pop(); int a = st.top(); st.pop(); if(str == "+") st.push(a + b); if(str == "-") st.push(a - b); if(str == "*") st.push(a * b); if(str == "/") st.push(a / b); } else st.push(stoi(str)); } return st.top(); }};
0 0
- [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 OJ] 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
- 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
- 外部中断的按键,软件滤波实现方法
- Servlet 404
- html5学习记录01:常见标签
- TextInputLayout,FloatingActionButton与Snackbar
- 欢迎使用CSDN-markdown编辑器
- [leetcode] 150. Evaluate Reverse Polish Notation 解题报告
- Unity3D如何有效地组织代码
- Android实际开发问题05------杀不死的服务
- 队列
- 新旧代码的兼容1
- 结构体、联合、枚举
- 学车记录
- iOS日期处理小结
- Spring4.0学习笔记003——Bean的配置一(基于XML文件)