[leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
- Division between two integers should truncate toward zero.
- The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
Example 1:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
思路
I would use stack to help solving this problem
Traverse the whole given string array
1. if operand is integer, push into stack


2. if operand is operation, pop two items from stack, do caculation and push result back to stack

code
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if(s.equals("+")) {
stack.push(stack.pop()+stack.pop());
}else if(s.equals("/")) {
int latter = stack.pop();
int former = stack.pop();
stack.push( former / latter);
}else if(s.equals("*")) {
stack.push(stack.pop() * stack.pop());
}else if(s.equals("-")) {
int latter = stack.pop();
int former = stack.pop();
stack.push(former - latter);
}else {
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
}
[leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法的更多相关文章
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
随机推荐
- MySQL Error--Error Code
mysql error code(备忘) 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导 ...
- 在flask框架中,对wtforms的SelectMultipleField的一个报错处理
先粘贴代码: form.py文件: users = SelectMultipleField( label="请选择用户", validators=[ DataRequired(&q ...
- Docker入门与实战讲解
转载自:http://blog.csdn.net/relax_hb/article/details/69668815 简述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包 ...
- django 路由系统中name应用
作用:对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL ***** name的两大应用 url(r'^asdfasdfasdf/', views.index, name= ...
- debian删除i386的包
sudo apt-get remove --purge `dpkg --get-selections | grep i386 | awk '{print $1}'`; sudo dpkg --remo ...
- CRM 模拟用户
web api 模拟用户 转:https://blog.csdn.net/vic0228/article/details/80649615 var req = new XMLHttpRequest() ...
- lumbda表达式初探
一.表达式格式定义 (parameters) -> expression 或 (parameters) ->{ statements; } 注意点:左边是输入参数,就相当于我们定义方法中的 ...
- solidworks建立三维模型里面的几何对象和工程图里面的元素的联系
本文是帮助里面的一个例子, 首先打开一个三维模型和对应的工程图,保持三维模型为当前激活窗口,在三维模型里面选中一个面或者一个边,然后运行下面的代码, 会将工程图里面的第一视图里面对应的投影元素的线型的 ...
- 自然语言处理NLP-云端API汇总
Google Google Cloud:https://cloud.google.com/natural-language/ ParallelDots ParallelDots, Inc. 无需训练, ...
- 可视化神器--Plotly
数据分析离不开数据可视化.我们最常用的就是pandas,matplotlib,pyecharts当然还有Tableau,看到一篇文章介绍plotly制图后我也跃跃欲试,查看了相关资料开始学习plotl ...