题目:

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. Ubuntu16.04安装JDK

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是 ...

  2. bootstrap中弹出窗体dialog的自定义

    感谢nakupanda的https://github.com/nakupanda/bootstrap3-dialog 根据需要弹出窗体,但是可以移动,不遮挡下面的内容,所以就修改了源代码,添加了一个属 ...

  3. Knockout.Js官网学习(text绑定)

    前言 text 绑定到DOM元素上,使得该元素显示的文本值为你绑定的参数.该绑定在显示<span>或者<em>上非常有用,但是你可以用在任何元素上. 简单绑定 Today's ...

  4. Centos 7配置ntp时间同步

    1.NTP时钟同步方式说明     NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步: 1)直接同步      使用ntpdate命令进行同步,直接进行时间变更.如果服务器上存在一个1 ...

  5. dedecms 根据key取得联动类型(enum)值

    ---恢复内容开始--- //$key:如城市的ID,$enum_file  data/enums中的文件 function get_enum_data($key, $enum_file)    {  ...

  6. Linux忘记密码的解救方法

    Linux版本 centos5.6 64bit 环境 vmware 忘记密码 解决方法1: 重启系统, 一.重启系统,在系统引导前按任意键进入菜单.如图:GRUB: 在引导装载程序菜单上,用上下方向键 ...

  7. 管理员 修改MySQL 5.7.9 新版本的root密码方法以及一些新变化整理

    MySQL 5.7版本开始,增强密码验证机制,网上说安装的时候会在/root/.mysql_secret  文件中生成默认密码,这一点自 5.7.6版本以后也去掉了. 针对如果生成默认密码,网上有一个 ...

  8. hudson项目中的运用

    项目中持续集成管理一直是用的hudson,最近的话,hudson遇到不少问题,因为之前对这个也不是很熟悉,所以也花了比较多的时间去解决,现在刚好也可以总结下自己学习到的hudson知识. 首先在我看来 ...

  9. Python判断是否是数字(无法判断浮点数)(已解决)

    s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...

  10. 菜鸟学习Struts——Scope属性

    一.概念. 在Action映射配置中,Scope属性可以取值为:request或session.Scope属性表示:Struts框架在将     ActionForm对象(与目标Action匹配的Ac ...