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 + ...
随机推荐
- android 重写系统进度条
转载自http://blog.csdn.net/codingandroid/article/details/8495074 自定义progressbar现在要自定义一个等待的时候转动的小圈,相信大家也 ...
- 2019.9.10附加题while练习
题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万之 ...
- iOS 内存管理arc
http://www.tekuba.net/program/346/ ios自动释放池(autoreleasepool #import <Foundation/Foundation.h> ...
- EL表达式多条件或判断用法
简单记录一EL表达式的判断用法 <c:if test="${(order.status == '06'&& order.type=='02') || (order.st ...
- IntelliJ IDEA和Eclipse设置JVM运行参数
打开 IDEA 安装目录,看到有一个 bin 目录,其中有两个 vmoptions 文件,需针对不同的JDK进行配置: 32 位:idea.exe.vmoptions64 位:idea64.exe.v ...
- CSS属性Display(显示)和Visibility(可见性)
隐藏一个元素可以通过把display属性设置为“none”,或把visibility属性设置为“hidden”.但是请注意,这两种方法会产生不同的效果. Visibility:hidden可以隐藏某个 ...
- c++第一次的个人作业
循环结构是c++中重要的结构用以实现代码的反复使用 三种不同的循环结构以及嵌套是必须认识与能够使用的 三种循环结构分别是: 在for循环中可以在for后的括号内加入初始条件,循环条件与参数变化.使得整 ...
- 解决某些手机RadioGroup中的RadioButton不居中(右移)问题
最近一直在忙一个项目,页面的基本框架类似于QQ那样,有底部导航栏的,遂采用的是RadioButton来实现的.本来一直在我的模拟器上测试,页面展示是没啥问题的,效果图如下: 可是,坑爹的事今天却发生了 ...
- 数据存储在哪里? Java是值传递还是引用传递?
寄存器 : 最快的存储区,位于处理器中,寄存器会按需求自行分配空间,java不能控制寄存器,所以在程序中感觉不到它的存在 栈(stack) : 位于RAM(内存)中,速度仅次于寄存器,存储对象的引用( ...
- Mule自带例子之loanbroker-simple
1 配置效果图 2 配置文件 <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:cxf=& ...