【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 ...
随机推荐
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- PowerDesigner中导出设计说明文档
点击下图的新建按钮,新建一个导出内容的模板 模板设计界面分为2栏,左边是可选的模板内容,右侧是模板,双击左侧条目会添加到右侧,最后生成的文件中就有此项内容. 已经添加到右侧的内容可以编辑,双击右侧的条 ...
- 如何为Kafka集群选择合适的Partitions数量
转载:http://blog.csdn.net/odailidong/article/details/52571901 这是许多kafka使用者经常会问到的一个问题.本文的目的是介绍与本问题相关的一些 ...
- Hadoop MapReduceV2(Yarn) 框架简介[转]
对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详的卓越开源分布式文件存储及处理框架,对于 Hadoop 框架的介绍在此不再累述,读者可参考 Hadoop 官方简介.使用和学习过老 H ...
- java之通过反射,来获得某对象的所有方法(类方法提取器)
参考Thinging in Java 在编程时, 如果不记得一个类是否有某个方法,或者不知道一个类究竟能做些什么,而又不想通过索引或 类的层次结构去查找jdk文档,这时通过反射的小工具能节省很多时间. ...
- 小记:获取系统时间的long值,格式化成可读时间。
/** * 返回的字符串形式是形如:2013年10月20日 20:58 * */ public static String formatTimeInMillis(long timeInMillis) ...
- 判断子元素(or属性)是否存在
if(typeof($("#aid").attr("rel"))=="undefined") 即可
- html5+js实现刮刮卡效果
通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: <!DOCTYPE html> <h ...
- IDEA调试javaScript
谈起JavaScript调试,大家可能想到的就是FireFox下的FireBug,这毫无疑问,FireBug基本已经成为JavaScript开发人员的必备工具.在本文中,将向大家介绍如 ...
- [转载]Android View.onMeasure方法的理解
2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...