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
理解这道题首先要理解什么是波兰表达式、什么是逆波兰表达式,这个表达式的意义何在:看参考链接;
参考链接:
Evaluate Reverse Polish Notation:https://www.cnblogs.com/dolphin0520/p/3708587.html
波兰式、逆波兰式与表达式求值:https://blog.csdn.net/linraise/article/details/20459751
思路:
我们熟悉的带括号的表达式,叫做中缀表达式,符合人们的直观感受。但是对于计算机来说,开销较大。
波兰表达式(后缀表达式)的意义在于不需要使用括号来决定哪个运算先运行,利用栈的特性来模拟计算,遍历这个逆波兰表达式数组,遇到操作数推进操作数的栈s;遇到操作符,将栈s顶两个操作数a和b取出进行操作符运算,最后将运算结果c放进栈中。
代码实现:
class Solution {
public:
int evalRPN(vector<string>& tokens)
{
stack<int> sk;
for(int i = ; i<tokens.size();i++)
{
if( tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/" )
{
if(sk.size()<)
return ;
int op1 = sk.top(); sk.pop();
int op2 = sk.top(); sk.pop();
int res = ;
if(tokens[i] == "+")
res = op1+op2;
if(tokens[i] == "-")
res = op2-op1;
if(tokens[i] == "*")
res = op1*op2;
if(tokens[i] == "/")
res = op2/op1;
sk.push(res);
}
else
sk.push(stoi(tokens[i]));
}
return sk.top();
}
};
LeetCode150_Evaluate Reverse Polish Notation评估逆波兰表达式(栈相关问题)的更多相关文章
- [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 +, -, ...
- [LintCode] 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 +, -, ...
- LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
题目: Evaluate Reverse Polish Notation Evaluatethe value of an arithmetic expression in Reverse Polish ...
- 逆波兰表达式[栈 C 语言 实现]
逆波兰表达式 逆波兰表达式又叫做后缀表达式.在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为中缀表示.波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示 ...
- Evaluate Reverse Polish Notation(堆栈)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- Evaluate Reverse Polish Notation leetcode java
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
随机推荐
- 【时光回溯】【JZOJ3571】【GDKOI2014】内存分配
题目描述 输入 输出 输出m行,每行一个整数,代表输入中每次程序变化后系统所需要的空闲内存单位数. 样例输入 2 3 1 4 1 4 2 2 1 2 1 1 1 1 1 样例输出 2 3 1 数据范围 ...
- ROS报错:IOError:[Errno 13]permission denied: /home/neousys/.ros/roscore-11311.pid"
在安装ROS后启动ROS,输入:roscore 时报错: 这个问题是由于该路径下ros文件权限造成的. 输入以下命令修改权限: sudo chmod -R ~/.ros/ 修改完成后再次输入rosco ...
- 阿里云MaxCompute 2019-4月刊
摘要: 4月新功能发布,精彩技术好文推荐,5月线上线下活动抢先知道,尽在4月刊. 您好,MaxCompute 2019.4月刊为您带来产品最新动态和丰富的产品技术内容,欢迎阅读. 导读 [功能发布]4 ...
- 消息点击率翻倍的背后——闲鱼无侵入可扩展IFTTT系统
一.面临问题 在闲鱼生态里,用户之间会有很多种关系.其中大部分关系是由买家触发,联系到卖家,比如买家通过搜索.收藏.聊天等动作与卖家产生联系:另外一部分是平台与用户之间的关系.对这些关系分析之后我们发 ...
- protobuf_1
我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google ...
- H3C Telnet配置例子
- 2011-04-21 运程连Oracle的方法
oracle无法远程连接重要原因,即使防火墙开放1521端口, 但是返回包可能是随机端口,所以仍有可能被防火墙阻止. 解决方法: 在注册表中增加一个字符串值如下.可解决 花费两天找到的方法 [HKEY ...
- 【codeforces 520B】Two Buttons
[题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...
- SQLSTATE[HY000] [2002] 错误
http://www.thinkphp.cn/topic/36194.html 使用tp框架 3.2.3 ,在windows上跑的时候没有任何问题,但是部署到linux系统和是哪个,就会报这个错,不知 ...
- HDU 2717 宽搜第一题、
题意:求n到k的最小路径, n有三种变法 n+1,n-1或者2*n: 贴个广搜的模版在这里把.... 总结一下:一般涉及到求最短路的话用宽搜 #include<iostream> #in ...