Leetcode - Evaluate Reverse Polish Notation
来源:互联网 发布:cg软件作用 编辑:程序博客网 时间:2024/06/05 00:55
初看貌似有点复杂,但是搞懂了非常简单,就一个简单的栈应用,每次遇到计算符号"+", "-", "*", "/"就将栈顶端两个数字出栈,计算后再将结果压栈即可。。
#include<iostream>#include<vector>#include<stack>using namespace std;class Solution {public:int evalRPN(vector<string> &tokens) {stack<int> stack;for (int i = 0; i < tokens.size(); i++){if (tokens[i] == "*" || tokens[i] == "-" || tokens[i] == "+" || tokens[i] == "/"){int val2 = stack.top();stack.pop();int val1 = stack.top();stack.pop();int tmpVal;if (tokens[i] == "*")tmpVal = val1 * val2;else if (tokens[i] == "-")tmpVal = val1 - val2;else if (tokens[i] == "+")tmpVal = val1 + val2;elsetmpVal = val1 / val2;stack.push(tmpVal);}else{int tmpVal = atoi(tokens[i].c_str());stack.push(tmpVal);}}return stack.top();}};
0 0
- 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
- 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
- LeetCode-Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- Leetcode:Evaluate Reverse Polish Notation
- Leetcode--Reorder List
- 第九届湖南省省赛
- 知乎上来的图
- Java编程最差实践
- Codeforces 127A-Wasted Time(题意)
- Leetcode - Evaluate Reverse Polish Notation
- python 执行 hadoop-2.2.0 mapreduce
- 【数据结构】顺序存储结构线性表C语言版
- 模拟大圆盘旋转抽奖的功能Demo
- 常见动态内存错误---内存泄漏
- STM32-Flash做为存储器储存数据
- Java内存溢出的一个小例子
- Best Time to Buy and Sell Stock II
- Word Ladder II