题目:

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. 使用plupload绕过服务器,批量上传图片到又拍云

    本文最初发布于我的个人博客:Jerry的乐园 综述 论坛或者贴吧经常会需要分享很多图片,上传图片比较差的做法是上传到中央服务器上,中央服务器再转发给静态图片服务器.而这篇文章讲介绍如何使用pluplo ...

  2. HTML 表单总结http://images2015.cnblogs.com/blog/1001203/201607/1001203-20160730200559841-2144892373.png

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. Cassandra 数据模型 (基于CQL,解决胖列数量限制及灵活性问题)(1.1及以上版本)

    文中主要交代Cassandra的编程模型及数据结构. 由于Cassandra版本数次更新,网上中文的资料已经有点过时,比较有代表性的比如ebuy那篇文章都已经过时了,于是自己找资料,结合官方博客写一篇 ...

  4. SAE平台的文件I/O处理

          用过SAE平台的朋友应该知道,出于平台安全性的考虑,SAE限制了用户对于本地IO的使用.但这样对于一些传统的PHP项目,也许带来了很多不便,因为它们都或多或少的有对本地IO的操作,像Sma ...

  5. C++求斐波那契数

    题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...

  6. 点击后弧形展开的炫酷菜单--第三方开源-- CircularFloatingActionMenu(一)

    CircularFloatingActionMenu在github上项目主页地址:https://github.com/oguzbilgener/CircularFloatingActionMenu ...

  7. Android VideoView简单播放视频

    给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例.(1) 需要在布局文件中写一个ViedoView: ...

  8. 苹果Mac OS系统shell命令大全介绍

    基本命令 1.列出文件 ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions 参数 -w 显示中文,-l 详细信息, -a 包括隐藏 ...

  9. Windows 8.1激活问题

    今天电脑开机,莫名的出现“你的Windows证书即将过期”. 系统本来用HEU_KMS_Activator 已激活成功,但查看系统激活状态时,结果如下图所示: 显示系统是已激活成功的.但还是有提示说系 ...

  10. 关联参数(&的用法)

    <?php header("Content-Type:text/html;charset=gb2312"); function test1(&$a){ $a.=&qu ...