leetcode150 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.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
计算逆波兰表达式,使用一个栈来存储操作数字,遍历输入的链表。
如果为数字,直接push到栈内,如果为操作符号,就把栈顶的两个元素pop出来运算一下。
运算的结果push到栈内。
public class Solution {
public int evalRPN(String[] tokens) {
int result = 0;
Stack<Integer> stack = new Stack<Integer>();
String operators = "+-*/";
for(int i=0; i<tokens.length;i++){
if (operators.contains(tokens[i])){
int b = stack.pop();
int a = stack.pop();
stack.push(calculate(a,b,tokens[i]));
}else{
stack.push(Integer.valueOf(tokens[i]));
}
}
result = stack.pop();
return result;
}
public int calculate(int a, int b, String operator){
char op = operator.charAt(0);
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
}
leetcode150 Evaluate Reverse Polish Notation的更多相关文章
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- WinForm------TreeList实现鼠标经过节点背景色改变
转载: http://www.cnblogs.com/zfanlong1314/archive/2012/06/26/2564124.html
- 关于Linux的内存(free -m)
查看Linux服务器下的内存使用情况,可以使用命令free -m.注意此命令只在Linux下有效,在FreeBSD中没有此命令.命令如下所示: used:已经使用的内存数 free:空闲的内存数 sh ...
- JavaScript排序算法——快速排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nodeJS接受post传过来的参数
1.nodeJs接受Post传递的参数需要通过绑定两个事件来获取, querystring = require("querystring"); 1 app.post('/comm ...
- apk支持html video控制 ,是播放状态就暂停,暂停状态就播放
apk支持html video控制 <!DOCTYPE html> <html> <body> <div style="text-align:cen ...
- Asp.NET的Trace追踪
http://m.blog.csdn.net/article/details?id=7026402 当我们扑捉程序错误时,调试器是开发者们最得力的助手.然而,ASP.NET的跟踪,在调试时是一个很棒的 ...
- linq多表join与group
var query =from a in this.ObjectContext.siteInfo join b in this.ObjectContext.shopInfo on a.siteID e ...
- DIOCP 运作核心探密
来自网友天地弦的DIOCP早已经广为人知了,有很多的同学都用上了它,甚至各种变异.修改版本也出了不少.我最近也在学习DIOCP,打算将它用于自己的服务端,今天让我们来一起探密它(DIOCP)的运作核心 ...
- jQuery -- is() 方法
定义和用法: 根据选择器.DOM元素或 jQuery 对象来检测匹配元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true.如果没有元素符合,或者表达式无效,都返回'false'. ''' ...
- phpcms使用细节
1.在模板中使用php语句 <?php for ($i=0; $i < 10; $i++) { echo $i."#######<br>"; }?& ...