LeetCode150: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
解题思路:
很简单的一题,直接利用栈实现,不多说了
实现代码:
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std; /*
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<string> operand_stack;
vector<string>::iterator iter;
for(iter = tokens.begin(); iter != tokens.end(); ++iter)
{
if(*iter == "+")//这里就不提取重复代码了,直接简单点
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
//int ret = atoi(op2.c_str()) + atoi(op1.c_str());
int ret = stoi(op2) + stoi(op1);//stoi为C++11才有
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "-")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) - atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "*")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) * atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "/")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
if( atoi(op1.c_str()) == )
return 0x7FFFFFF;
int ret = atoi(op2.c_str()) / atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else
operand_stack.push(*iter);
}
return atoi(operand_stack.top().c_str()); } }; int main(void)
{
string strs[] = {"", "", "", "/", "+"};
vector<string> tokens(strs, strs+);
Solution solution;
int ret = solution.evalRPN(tokens);
cout<<ret<<endl;
return ;
}
LeetCode150:Evaluate Reverse Polish Notation的更多相关文章
- 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 OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- 【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练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 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 ...
随机推荐
- Metropolis(多源点最短路)
Metropolis https://www.nowcoder.com/acm/contest/203/I 题目描述 魔方国有n座城市,编号为.城市之间通过n-1条无向道路连接,形成一个树形结构. 在 ...
- adf常用方法总结
1.使用clientAttribute传值.获取值 或组件上面放客户端属性 <af:selectBooleanCheckbox text="" label="&qu ...
- 搭建Eureka集群
1.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- pip常用操作指令
1.安装模块 pip install vitualenv pip install -r requirement.txt 2.查询模块信息 pip show pip 3.显示已经安装的模块 pip li ...
- Luogu 3959 [NOIP2017] 宝藏
NOIP2017最后一道题 挺难想的状压dp. 受到深度的条件限制,所以一般的状态设计带有后效性,这时候考虑把深度作为一维,这样子可以保证所有状态不重复计算一遍. 神仙预处理:先处理出一个点连到一个集 ...
- Ubuntu12.04下搭建Java环境
1.认识需要配置的环境变量 1). PATH: 作用是指定命令搜索路径,打开/etc/environment可以看到PATH变量的值,该变量包含了一系列的路径.那些路径都是一些经常使用的系统命令的目录 ...
- IPEndPoint
.NET框架用IPEndPoint 对象来表示一个特定的IP地址和端口的组合,应用该对象的场景多是在讲socket绑定到本地地址或者将socket绑定到非本地地址.
- Jmeter从一个Reply Message中获取N个参数的值,然后根据这个参数对后面的操作循环N次(ForEach Controller的用法)
假设Reply Message是这样的: <root><result code="0" msg="success" /><m k= ...
- Sublime Text webstorm等编译器快速编写HTML/CSS代码的技巧
<!DOCTYPE html> Sublime Text webstorm等编译器快速编写HTML/CSS代码的技巧--summer-rain博客园 xiayuhao 东风夜放花千树. 博 ...
- 【Win】使用ScreenToGif制作gif动态图片
ScreenToGif 经常要写各类教程,有时候需要制作一些演示动画,GIF动画图片是个不错的选择,ScreenToGif是一款GIF录屏软件,下载地址可自行百度. 运行环境 操作系统:windows ...