LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
题目: Evaluate Reverse Polish Notation
Evaluatethe value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["", "", "+", "", "*"] -> (( + ) * ) ->
["", "", "", "/", "+"] -> ( + ( / )) ->
简单题,借助一个stack就可以实现,将数值入栈,遇操作符时将两个数值出栈,计算后再入栈,如此即可实现。
int evalRPN(vector<string> &tokens)
{
stack<int> stokens;
for (int i = ; i < tokens.size(); ++ i) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int x1 = stokens.top();
stokens.pop();
int x2 = stokens.top();
stokens.pop();
int x3 = caculate(x1, x2, tokens[i]); //计算之后再入栈
stokens.push(x3);
}
else {
stokens.push(atoi(tokens[i].c_str()));
}
}
return stokens.top();
}
int caculate(int x1, int x2, string s)
{
int result;
if (s == "+")
result = x1 + x2;
else if (s == "-")
result = x1 - x2;
else if (s == "*")
result = x1 * x2;
else {
if (x1 >= x2)
result = x1 / x2;
else
result = x2 / x1;
}
return result;
}
LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium的更多相关文章
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode150_Evaluate Reverse Polish Notation评估逆波兰表达式(栈相关问题)
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+, ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- [leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- Could not find a package,configuration file provided by "G2O" ,G2OConfig.cmake,g2o-config.cmake
因为项目需要使用到g2o,所以自己从git上面clone下来, git clone https://github.com/RainerKuemmerle/g2o.git 然后: cd g2o mkdi ...
- R语言-线图(二)
1.线图示例 plot()为高水平作图命令,axis().lines().legend()都为低水平作图命令 > rain<-read.csv("cityrain.csv&q ...
- JAVA远程调试
1.远程端启动必须添加jvm参数 -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=${debug_port} 其中de ...
- idea导入eclipse项目的配置
idea导入eclipse项目需要的配置 1.配置jdk,这两处选择一样就可以,也可以根据自己需求选择,上边的比下边的版本高就行 2.这里会默认和配置jdk一样 3.添加lib依赖,选择到项目的lib ...
- Jmeter创建一个web测试计划
1. 下载Jmeter 下载地址:http://jmeter.apache.org/download_jmeter.cgi 下载后解压到你想“安装”的路径下,比如: D:\Program Files ...
- ubuntu中运行java程序
查找jdk rivsidn@rivsidn:~/demo/java$ sudo apt-cache search jdk default-jdk - Standard Java or Java com ...
- boost asio 学习(五) 错误处理
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6 5. Erro ...
- centos7下载
http://archive.kernel.org/centos-vault/7.0.1406/isos/x86_64/
- 20175316盛茂淞 2018-2019-2《Java程序设计》第4周学习总结
20175316盛茂淞 2018-2019-2<Java程序设计>第4周学习总结 教材学习内容总结 第五章 子类与继承 一.继承 1.继承定义:避免多个类间重复定义共同行为 2.子类与父类 ...
- explain 类型分析
all 全表扫描 ☆ index 索引全扫描 ☆☆ range 索引范围扫描,常用语<,<=,>=,between等操作 ☆☆☆ ref 使用非唯一索引扫描或唯一索引前缀扫描,返回单 ...