题目:

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评估逆波兰表达式(栈相关问题)的更多相关文章

  1. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium

    题目: Evaluate Reverse Polish Notation Evaluatethe value of an arithmetic expression in Reverse Polish ...

  6. 逆波兰表达式[栈 C 语言 实现]

    逆波兰表达式 逆波兰表达式又叫做后缀表达式.在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为中缀表示.波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示 ...

  7. Evaluate Reverse Polish Notation(堆栈)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  9. Evaluate Reverse Polish Notation leetcode java

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

随机推荐

  1. iOS app 设计推荐

    见微知著,谈移动缺省页设计 http://www.cocoachina.com/design/20150303/11186.html Facebook产品设计总监!设计APP时的14个必考题 http ...

  2. Gartner:阿里云位列全球云数据库市场份额前三,数据库未来需上云

    近日,国际权威研究机构Gartner发布 <The Future of the Database Management System (DBMS) Market Is Cloud>报告,鲜 ...

  3. C# 显示实现接口

    显示实现接口的目的就是为了同名方法. 接口是多实现的,比如说一个方法要实现多个接口,然后这几个接口中有同名方法,这个时候就用到了接口的显示实现. 显示实现接口 成员方法的调用: 接口名.方法名  访问 ...

  4. (转)理解inode

    作者:阮一峰 原文链接:http://www.ruanyifeng.com/blog/2011/12/inode.html 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上 ...

  5. 2019-11-20-dotnet-Blazor-用-C#-控制界面行为

    title author date CreateTime categories dotnet Blazor 用 C# 控制界面行为 lindexi 2019-11-20 18:26:25 +0800 ...

  6. mysql 忘记root密码的处理办法

    参考地址: https://blog.csdn.net/vv19910825/article/details/82979563 1.修改配置文件mysql\bin\my.ini 在文本  [mysql ...

  7. 15-8 pymysql的使用

    一 安装pymysql模块 1 pycharm安装 file-setting如图:然后点加号,搜索pymsql即可,点击安装 2 pip 安装 pip3 install pymysql 二  连接数据 ...

  8. oracle函数 SUBSTR(c1,n1[,n2])

    [功能]取子字符串 [说明]多字节符(汉字.全角符等),按1个字符计算 [参数]在字符表达式c1里,从n1开始取n2个字符;若不指定n2,则从第y个字符直到结束的字串. [返回]字符型 [示例] SQ ...

  9. Java容易搞错的知识点

    一.关于Switch 代码: Java代码 1         public class TestSwitch { 2             public static void main(Stri ...

  10. 洛谷 2279 [HNOI2003]消防局的设立

    Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了 ...