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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- mybatis的逆向工程
mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说 ...
- 【原】flux学习笔记
最近React(web/native)依旧如火如荼,相信大家都跃跃欲试,入职新公司,现在的团队也开始在React领域有所尝试. 2016年应该是React 逐渐走向成熟的一年.之前在原来公司搞不懂的问 ...
- 使用ThreadLocal、Apache的dbutils的QueryRunner和dbcp2数据库连接池的BasicDataSource封装操作数据库工具
package hjp.smart4j.framework.helper; import hjp.smart4j.framework.util.CollectionUtil; import hjp.s ...
- ubuntu下安装wine1.8和阿里旺旺
参考:http://www.linuxidc.com/Linux/2015-12/126722.htm和http://www.linuxidc.com/Linux/2016-05/131131.htm ...
- JavaWeb学习笔记——JavaEE基础知识
- HTML学习笔记——post表单
1>form1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- python 内建函数setattr() getattr()
python 内建函数setattr() getattr() setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为v ...
- HDU 1326 Box of Bricks(水~平均高度求最少移动砖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...
- ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】
ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...
- Xunsearch 中文全文搜索
原文地址:http://www.yiichina.com/code/661 官网地址:http://www.xunsearch.com/ 1.安装 wget http://www.xunsearch. ...