2、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
//判断字符串是否是数字,如果是就压栈,不是就弹出2个元素进行计算,将计算结果压栈
//最后栈顶元素即为所求class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> st;
for (int i = ; i < tokens.size(); i++){
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int temp = ;
int a = st.top();
st.pop();
int b = st.top();
st.pop();
if (tokens[i] == "+") { temp = b + a; }
else if (tokens[i] == "-") { temp = b - a; }
else if (tokens[i] == "*") { temp = b * a; }
else { temp = b / a; }
st.push(temp);
}
else {
st.push(stoi(tokens[i]));
}
}
return st.top();
}
};
2、evaluate-reverse-polish-notation的更多相关文章
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- 『高性能模型』深度可分离卷积和MobileNet_v1
论文原址:MobileNets v1 TensorFlow实现:mobilenet_v1.py TensorFlow预训练模型:mobilenet_v1.md 一.深度可分离卷积 标准的卷积过程可以看 ...
- zzw原创_解决Could not chdir to home directory /test/bdctool: Permission denied一例
1.用命令新建一用户 adduser -d /test/bdctool -m bdctool 2.用新用户登录,报错:Could not chdir to home directory /test/ ...
- 【转】ASP.NET Core开发之HttpContext
ASP.NET Core中的HttpContext开发,在ASP.NET开发中我们总是会经常用到HttpContext. 那么在ASP.NET Core中要如何使用HttpContext呢,下面就来具 ...
- mfs windows客户端
之前用moosefs,苦于没有Windows客户端, 本来想在Linux上mfsmount一个目录然后用samba做共享目录,但是这样太不简洁了. 当然豆瓣上有的小伙伴也说自己开发了moosefs的W ...
- STL 小白学习(2) string
#include <iostream> using namespace std; #include <string> //初始化操作 void test01() { //初始化 ...
- Android 音视频深入 十三 OpenSL ES 制作音乐播放器,能暂停和调整音量(附源码下载)
项目地址https://github.com/979451341/OpenSLAudio OpenSL ES 是基于NDK也就是c语言的底层开发音频的公开API,通过使用它能够做到标准化, 高性能,低 ...
- 数位dp讲解及模板
转载自:传送门 数位DP其实是很灵活的,所以一定不要奢求一篇文章就会遍所有数位DP的题,这一篇只能是讲清楚一种情况,其他情况遇到再总结,在不断总结中慢慢体会这个思想,以后说不定就能达到一看到题目就能灵 ...
- Python *Mix_w4
1.什么是列表(list) 列表是一个可变的数据类型 列表由[ ]来表示,每一项元素使用逗号隔开.列表什么都能装,能装对象的对象. 列表可以装大量的数据,列表能装所有数据类型 2.列表的索引和切片 列 ...
- kaptcha验证码插件使用与参数
kaptcha使用1 kaptcha使用2 kaptcha使用3 kaptcha参数说明
- ionic2一个需求模块的文件该是这样子的
1.组件文件夹的文件 除了index.ts 其他文件为各个页面组件文件 2.index.ts的代码如下 import {SecurityFinance} from "./security-f ...