题目:

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<int> sta;
for ( size_t i = ; i < tokens.size(); ++i )
{
if ( tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/" )
{
int right = sta.top();
sta.pop();
int left = sta.top();
sta.pop();
if ( tokens[i]=="+" ) { sta.push(left+right); continue; }
if ( tokens[i]=="-") { sta.push(left-right); continue; }
if ( tokens[i]=="*") { sta.push(left*right); continue; }
if ( tokens[i]=="/") { sta.push(left/right); continue; }
}
else
{
sta.push(atoi(tokens[i].c_str()));
}
}
return sta.top();
}
};

tips:

堆栈求逆波兰表达式口诀:遇上数字进栈;遇上操作符先出栈两个元素,计算结果后再压入栈。

======================================================

第二次过这道题,思路记得比较清楚。这里需要记住一个函数c++ atoi (string 转 int),这样在读入的时候转一次就够了。

stack里面存的是数字。操作符不进栈。

class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> sta;
for ( int i=; i<tokens.size(); ++i )
{
if ( tokens[i]=="+" || tokens[i]=="-" || tokens[i]=="*" || tokens[i]=="/" )
{
int right = sta.top(); sta.pop();
int left = sta.top(); sta.pop();
if ( tokens[i]=="+" )
{
sta.push(right+left);
}
else if ( tokens[i]=="-")
{
sta.push(left - right);
}
else if ( tokens[i]=="*" )
{
sta.push(right * left);
}
else
{
sta.push(left / right);
}
}
else
{
sta.push(atoi(tokens[i].c_str()));
}
}
return sta.top();
}
};

【Evaluate Reverse Polish Notation】cpp的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  3. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

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

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

  7. 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 ...

  8. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  9. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. Android开发教程大全介绍

    Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...

  2. ASP.NET相关技术整理

  3. Windows下安装Elasticsearch

    1.下载elasticsearch-1.6.0 .jdk-7u67-windows-x64.exe 1.6.0必须用jdk1.7才能运行 2.配置JAVA_HOME:C:\Program Files\ ...

  4. azure注册码

    用户名:aaa 注册秘钥:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajx 注册用户名:www.yuanxingku.com ...

  5. 最新hosts,更新hosts,可用

    点击这里,全选后复制,粘贴到C:\Windows\System32\drivers\etc的hosts里面,把原来的置换了

  6. MongoDB探索之路(一)——入门

    1.MongoDB和传统关系型数据库的比较  2.面向文档的 NoSQL 数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能.  3.MongoDB可以作为日志分 ...

  7. 03-树2 List Leaves

    二叉树及其遍历 一遍AC,挺开心的hhh~ 简单讲下思路:叶子,顾名思义就是没有左右子树的结点.由于题目要求,叶子结点的输出顺序是从上往下,从左往右.所以用层序遍历法. 当然,这里先找到root树的根 ...

  8. 使用 Attribute +反射 来对两个类之间动态赋值

    看同事使用的 一个ORM 框架 中 有这样一个功能  通过特性(附加属性)的功能来 实现的两个类对象之间动态赋值的 功能 觉得这个功能不错,但是同事使用的 ORM 并不是我使用的  Dapper  所 ...

  9. python 核心编程第二版 课后习题 第11章

    11-3 函数.在这个练习中,我们将实现 max()和 min()内建函数. (a) 写分别带两个元素返回一个较大和较小元素,简单的 max2()核 min2()函数.他们应该可以用任意的 pytho ...

  10. 我的WPF控件库——KAN.WPF.XCtrl(141105)

    自己开发的WPF控件库,只是初版,有扩展的Button,TextBox,Window.详细参见前几篇博文. WPF自定义控件(一)——Button:http://www.cnblogs.com/Qin ...