题目:

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. Dubbo简单理解

    Dubbo 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,Dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有Dubbo这样 ...

  2. java.lang.IllegalArgumentException: Service not registered

    java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...

  3. 前端基础之Bootstrap

    1. 页面加载完之后才执行的JS代码        1. DOM方式            window.onload = function(){}        2. jQuery方式       ...

  4. Intersection(Check)

    Intersection http://poj.org/problem?id=1410 Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  5. linux中使用locate搜索文件方法记录

    在linux中,有时用apt或者yum等软件包管理工具直接安装软件的时候,不知道软件到底安装到哪里去了,配置文件放哪里?这个时候就可以使用搜索命令locate来找到这些文件.海词上locate翻译为找 ...

  6. 41-json.decoder.JSONDecodeError: Invalid control character at: line 6894 column 12 (char 186418)

    在使用python中将单词本的单词用正则匹配成字典后,以json存储,仪json读入,但是一直报错: 原因是: 正则处理后的数据有的出了点问题,导致一个字典的 有多个相同的键!!!,则肯定会报错啊!! ...

  7. Oracle增加一列、修改一列数据类型

    Oracle增加一列.修改一列数据类型: 添加一列: alter   table   A   add( CFYJSNR  varchar2(20)); 修改列: alter  table A  ren ...

  8. Scrum 团队成立 -- 软件工程

      团队项目选题  : 金融工具:复利计算与投资记录项目继续升级,开发定位明确.功能专注的工具类软件 团队队员 : 蔡舜 , 林宇粲 , 王昕明 , 卢晓洵 团队目标 : 不断完善 团队口号 : 永不 ...

  9. linux信号处理总结

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

  10. 全基因组测序 从头测序(de novo sequencing) 重测序(re-sequencing)

    全基因组测序 全基因组测序分为从头测序(de novo sequencing)和重测序(re-sequencing). 从头测序(de novo)不需要任何参考基因组信息即可对某个物种的基因组进行测序 ...