题目:

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

解题思路:

很简单的一题,直接利用栈实现,不多说了

实现代码:

#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std; /*
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<string> operand_stack;
vector<string>::iterator iter;
for(iter = tokens.begin(); iter != tokens.end(); ++iter)
{
if(*iter == "+")//这里就不提取重复代码了,直接简单点
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
//int ret = atoi(op2.c_str()) + atoi(op1.c_str());
int ret = stoi(op2) + stoi(op1);//stoi为C++11才有
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "-")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) - atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "*")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
int ret = atoi(op2.c_str()) * atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else if(*iter == "/")
{
string op1 = operand_stack.top();
operand_stack.pop();
string op2 = operand_stack.top();
operand_stack.pop();
if( atoi(op1.c_str()) == )
return 0x7FFFFFF;
int ret = atoi(op2.c_str()) / atoi(op1.c_str());
string result = to_string(ret);
operand_stack.push(result); }
else
operand_stack.push(*iter);
}
return atoi(operand_stack.top().c_str()); } }; int main(void)
{
string strs[] = {"", "", "", "/", "+"};
vector<string> tokens(strs, strs+);
Solution solution;
int ret = solution.evalRPN(tokens);
cout<<ret<<endl;
return ;
}

LeetCode150:Evaluate Reverse Polish Notation的更多相关文章

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

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

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

  3. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  4. 【leetcode】Evaluate Reverse Polish Notation

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

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

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

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

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

  7. leetcode - [2]Evaluate Reverse Polish Notation

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

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

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

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

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

随机推荐

  1. Educational Codeforces Round 59

    B. Digital root 题意: 题目定义了x的digital root是S(x).S(5)=5,S(38)=S(3+8=11)=S(1+1+2)=2. 有n个询问,每次询问给出ki和xi,要你 ...

  2. 72. Edit Distance (String; DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. Web服务技术协议:REST与SOAP

    Web服务技术就有SOAP(Simple Object Access Protocol,简单对象访问协议)和REST(Representational State Transfer,表示性状态转移) ...

  4. iOS 处理cell选中时背景颜色消息问题

    在cell上添加子控件,在我们点击或者长按的时候,如果子控件有背景颜色,这时候背景颜色就会没有了,这个时候产品经理过来一顿怼,

  5. linux信号处理总结

    本文主要讲解常见信号的处理方式. Sighup:终端关闭时,发送给此会话的所有进程组.Setsid成功后不再属于该会话,收不到该消息. Sigterm:  kill process_id时产生. Si ...

  6. windows驱动

    DriveEntry() 启动 停止 接口函数 驱动程序名 驱动程序版本号 异常处理 是否运行 声明LPOReceive类型的函数 声明函数指针(外部传进来的回调函数) 存放配置字符串 本机IP 串口 ...

  7. lnmp vhost 虚拟目录配置

    以前常用Windows 很熟悉,lnmp 配置虚拟目录也很简单. 安装完lnmp环境之后,在nginx的配置文件夹下,我采用的方法是复制default.conf 然后重命名为vhost_a.conf ...

  8. jetty无法即时更新html、js、css等静态文件的解决办法

    网上搜了一大堆方法.最常见的是找到jetty\etc\webdefault.xml文件,找到useFileMappedBuffer参数,把true改为false <init-param> ...

  9. javascript的一些札记

    1. 原来放在不同js文件里面的$(document).ready(function(){})都会执行到. 2. $(window).scroll(function(){})  窗口滚动事件. 3.  ...

  10. DB2自增长ID

    建议类似的应用采用sequence对象,将来的应用维护和数据迁移会很方便.考虑的因素较少. 对于序列可以使用nextval和prevval来获得下一个和上一个值:CREATE SEQUENCE seq ...