Problem link:

http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/


According to the wiki page, the algorithm for evaluating any postfix expression is fairly straightforward. The following is the pseudo-code for evaluating a postfix expression.

while there are input tokens left
read the next token from input
if the token is a value (operand)
push it into the stack
else (the token is an operator)
let n be the number of the arguments of the operator
if there are fewer than n values in the stack
return ERROR (non-sufficient operands in the expression)
else
pop the top n values from the stack
evaluate the expression with the operator and operands
push the result, if any, back into the stack
if there is only one value in the stack
return the value as the result
else
return ERROR (too many operands in the expression)

However, in this problem, it is much easier since there are only four operators. What we need to concern is only to identify the operators and numbers (integer in this case). Therefore, we will implement two functions:

  • A stack structure
  • Functions on a string that determine the string is an integer or an operator.

The C++ implementation of the algorithm is as follows.

#include <stack>
#include <ctype.h> class Solution {
public:
int evalRPN(vector<string> &tokens) {
// The stack to store operators (int values) only
std::stack<int> res;
int x,y;
// Iterate the vector from left to right
for(std::vector<string>::iterator it = tokens.begin(); it != tokens.end(); it++)
{
// If operator, pop and evaluate
if ( (*it).length() == 1 && !isdigit((*it)[0]) ) {
y = res.top();
res.pop();
x = res.top();
res.pop();
if (*it == "+") x += y;
else if (*it == "-") x -= y;
else if (*it == "*") x *= y;
else if (*it == "/") x /= y;
res.push(x);
}
// If operand, push it into the stack
else res.push( atoi((*it).c_str()) );
}
// The only value left in the stack is the final result
return res.top();
}
};

【LeetCode OJ】Evaluate Reverse Polish Notation的更多相关文章

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

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

  2. LeetCode OJ 150. Evaluate Reverse Polish Notation

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

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

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

  4. 【leetcode】Evaluate Reverse Polish Notation

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

  5. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  6. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  7. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

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

  8. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  9. 【LeetCode】150. Evaluate Reverse Polish Notation

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

随机推荐

  1. CentOS 7.x安装配置

    简述 VMware可以创建多个虚拟机,每个虚拟机上都可以安装各种类型的操作系统.安装方法也有很多种.下面,主要以ISO镜像安装为例,介绍CentOS 7.x的安装过程及相关的参数设置. 简述 创建虚拟 ...

  2. IOS开发设计思路

    我在做 iOS 开发的时候,发现自己在写程序的时候,常常处于两种状态的切换,我把这两种状态称为软件开发的上帝模式与农民模式.我先给大家介绍一下这两种模式的特点. 上帝模式 处于上帝模式时,我需要构思整 ...

  3. 关于html中table表格tr,td的高度和宽度

    关于html中table表格tr,td的高度和宽度 关于html中table表格tr,td的高度和宽度 做网页的时候经常会遇到各种各样的问题,经常遇到的一个就是会碰到表格宽度对不齐的问题.首先,来分析 ...

  4. hadoop主节点(NameNode)备份策略以及恢复方法

    link:http://jiajun.iteye.com/blog/809125 一.dits和fsimage 首先要提到两个文件edits和fsimage,下面来说说他们是做什么的. 集群中的名称节 ...

  5. template模版与Underscore.js

    template模版与Underscore.js 在项目中经常使用的模版是Underscore这个js框架的实用功能. 在html里面设定模板,然后js绑定数据,这样能避免在js中出现非常多的html ...

  6. php header setcookie headers_sent函数 函数检查 HTTP 标头是否已被发送以及在哪里被发送

    这里需要注意的 header() 最常被拿來送 header('Location: /'); 等等, 做网页重定向的动作. 在使用 setcookie(), header()... 等函数前 不可以用 ...

  7. Git 提交大文件提示 fatal: The remote end hung up unexpectedly

    使用gitlab搭建的git server,如果直接使用http的方式去提交的话,提交小文件不会有问题,但是提交大文件时,会出错: fatal: The remote end hung up unex ...

  8. heartbeat安装

    wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5 ...

  9. 客户端访问AIDLService(远程绑定Service)

    import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android. ...

  10. sql 查询强制使用HASH连接性能测试比较

    HASH JOIN 散列连接 hash join是CBO 做大数据集连接时的常用方式.优化器扫描小表(或数据源),利用连接键(也就是根据连接字段计算hash 值)在内存中建立hash表,然后扫描大表, ...