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. Redis 学习资料整理

    菜鸟爬坑--Redis学习与探索(二):Redis的数据类型 http://www.cnblogs.com/codediary/archive/2015/02/20/redisstudy-2.html ...

  2. 读书笔记3 Socket

    Socket被称为网络插座.用于两个网络应用程序之间的通信. 通信地址:URI 通过协议,地址,端口号可以确定网络上的一个程序.地址和端口号组合称之为端点. 通常会有发信人通信地址,收信人通信地址这两 ...

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

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

  4. C语言实现进制转换

    #include<stdio.h> int main() {     char ku[16]={'0','1','2','3','4','5','6','7','8','9','A','B ...

  5. HTML第二部分表单及使用Photoshop快速制作网页

    一.表单 <form id="" name="" method="post/get" action="负责处理的服务端&qu ...

  6. 高度30px,宽度自适应,点线在文字中间

    <style>  .div{ position: relative; width: 100%; height: 30px; background: #ffff00}        .div ...

  7. firefox hack

    @-moz-document url-prefix(){ css选择器 { css样式设置 } }

  8. Codeigniter 集成sphinx搜索 这里采用的是coreseek中文搜索引擎,具体安装请参考官方网站

    先上效果图 加入sphinx类库(/application/libraries/sphinx_client.php) 0001 <?php 0002 0003 // 0004 // $Id: s ...

  9. 深入理解Redis:命令处理流程

    Redis是著名的NoSQL键值数据库服务器,为了保证效率,其数据都缓存在内存中.与Memcached相比,Redis支持的数据类型更多,包括String,List,Set,Zset和Hash.下面简 ...

  10. ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置

    核心提示:大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有 ...