题目:

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. C# split分割多个字符

    string[] myAgent = agentInfo.Split(new string[] { "$#$" }, StringSplitOptions.None);

  2. Mybatis中的N+1问题与延迟加载

    0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...

  3. java非常好用的读取文件的流的代码

    学过java的都知道java中有非常多的读取文件流的操作.这个要回到javase的io操作了.io流说实话,初学者学的肯定会非常混乱,那么多流,什么输入流,输出流,什么文件流,什么字节流,等等.我在这 ...

  4. Segments(叉积)

    Segments http://poj.org/problem?id=3304 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

  5. 条款2:尽量以const, enum, inline替换#define

    原因: 1. 追踪困难,由于在编译期已经替换,在记号表中没有. 2. 由于编译期多处替换,可能导致目标代码体积稍大. 3. define没有作用域,如在类中定义一个常量不行. 做法: 可以用const ...

  6. [leetcode]277. Find the Celebrity谁是名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  7. ajax.beginform控制器中实体为null的问题

    控制器: 函数声明:public JsonResult ApplyFun(Test test) 原因:在视图中有一个表单的name属性为test,因为冲突所导致.

  8. item2

    一.简介 iTerm2 是 OS X 下一款开源免费的的终端工具,很多人基本用它替代了原生的 Terminal.   二.特色功能 https://www.zhihu.com/question/274 ...

  9. Laravel中用GuzzleHttp

    阅读数:14715 今天项目中用到GuzzleHttp,开始不知道怎么用,其实还是很简单的. 直接在项目根目录,输入以下命令 composer require guzzlehttp/guzzle 1 ...

  10. XAMPP Apache + MariaDB + PHP + Perl

    https://www.apachefriends.org/zh_cn/index.html 什么是XAMPP? XAMPP是最流行的PHP开发环境 XAMPP是完全免费且易于安装的Apache发行版 ...