【LeetCode OJ】Evaluate Reverse Polish Notation
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的更多相关文章
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- JavaWeb基础: Web应用和Web服务器
Web Server工作原理 假设工程师想提供一个网页浏览的Web应用给客户,需要经过以下几步: 在指定目录下新建资源(hello.html) 编写一个服务器ServerDemo监听请求和响应请求:S ...
- (30)odoo中的快捷标签
* 快捷标签 提供快捷标签是为了简化代码的编码,把复杂的工作封装化 * 找到封装化的源码: openerp/tools/convert.py xml_import self._ ...
- hduoj-----(2896)病毒侵袭(ac自动机)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- 百度 WebUploader 简单入门示例
首先一定要引入:jquery.js 然后是webuploader的一个 css和还一个js 三个必须引用. <!DOCTYPE html> <html xmlns="htt ...
- think in java 读书笔记 1 ——移位
目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 在Think in Java中 ...
- form表单提交过程
本文为转载文章! 今天,我将站在HTML和单纯的Asp.net框架的角度来解释它们的工作方式,因此,本文不演示WebForms服务器控件的相关内容. 简单的表单,简单的处理方式 好了,让我们进入今天的 ...
- 修改WAMPServer中MySql中文乱码的方法
修改MySql的配置文件my.ini,在[client]段落增加:default-character-set=utf8;在[mysqld]段落增加:character_set_server=utf8; ...
- hibernate generator class=xxx id详解
<id>元素中的<generator>用来为该持久化类的实例生成唯一的标识,hibernate提供了很多内置的实现.Increment:由hibernate自动递增生成标识符, ...
- ASP.net 验证码(C#) MVC
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...