[Leetcode] 150. Evaluate Reverse Polish Notation 解题报告
来源:互联网 发布:网络ip地址冲突 编辑:程序博客网 时间:2024/06/06 04:04
题目:
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 (int i = 0; i < tokens.size(); ++i) { if (isOperator(tokens[i])) { int second = st.top(); st.pop(); int first = st.top(); st.pop(); st.push(getValue(first, second, tokens[i][0])); } else { st.push(stoi(tokens[i])); } } return st.top(); }private: bool isOperator(const string &s) { if (s == "+" || s == "-" || s == "*" || s == "/") { return true; } else { return false; } } int getValue(int first, int second, char c) { if (c == '+') { return first + second; } else if (c == '-') { return first - second; } else if (c == '*') { return first * second; } else if (c == '/') { return first / second; } else { return -1; } }};
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
- 解决oracle表锁死
- 李白买酒的递归求法
- Java实现的二分查找算法
- ORACLE事物隔离级别
- Palindrome Linked List
- [Leetcode] 150. Evaluate Reverse Polish Notation 解题报告
- 第一行代码第二版(郭霖著)笔记之第六章(详解持久化技术)
- BZOJ1585 Earthquake Damage 2 地震伤害
- 【读书笔记】机器学习实战 6.3节 SVM-简化版SMO算法
- 通过java.net.URLConnection发送HTTP请求的方法
- linux route cache相关变迁
- Android AppCompatActivity去掉标题栏,全屏
- Sqlserver命令备份恢复数据库
- 对一些 关键词、语句、函数、专业名词 等的解释和理解(一)