150.Evaluate Reverse Polish Notation---逆波兰式求值
题目大意:计算逆波兰表达式的值。
法一:stack,用stack存数,遇到操作符,则运算。代码如下(耗时12ms):
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<Integer>();
for(int i = 0; i < tokens.length; i++) {
//如果是数值
if(!tokens[i].equals("+") && !tokens[i].equals("-") && !tokens[i].equals("*") && !tokens[i].equals("/")) {
s.push(Integer.parseInt(tokens[i]));
}
//如果是操作符
else {
int b = s.pop();
int a = s.pop();
int c = compute(a, b, tokens[i]);
s.push(c);
}
}
return s.pop();
}
//运算
private static int compute(int a, int b, String s) {
if(s.equals("+")) {
return a + b;
}
else if(s.equals("-")) {
return a - b;
}
else if(s.equals("*")) {
return a * b;
}
else {
return a / b;
}
}
法二:stack+异常。很新颖的异常用法。代码如下(耗时70ms):
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<Integer>();
for(int i = 0; i < tokens.length; i++) {
//捕捉异常,来辨别是数值还是操作符
try {
s.push(Integer.parseInt(tokens[i]));
}
catch(Exception e) {
int b = s.pop();
int a = s.pop();
int c = compute(a, b, tokens[i]);
s.push(c);
}
}
return s.pop();
}
private static int compute(int a, int b, String s) {
if(s.equals("+")) {
return a + b;
}
else if(s.equals("-")) {
return a - b;
}
else if(s.equals("*")) {
return a * b;
}
else {
return a / b;
}
}
150.Evaluate Reverse Polish Notation---逆波兰式求值的更多相关文章
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: 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 +, -, ...
- [leetcode]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 +, -, ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
随机推荐
- JVM——九大工具助你玩转Java性能优化
本文转载自 http://www.importnew.com/12324.html 本文由 ImportNew - 陈 晓舜 翻译自 idrsolutions.欢迎加入翻译小组.转载请参见文章末尾的要 ...
- TouTiao开源项目 分析笔记19 问答内容
1.真实页面预览 1.1.成果预览 首先是问答列表 然后每个item设置点击事件,进入问答内容列表 然后每一个问答内容也设置点击事件,进入问答详情 1.2.触发事件. 在WendaArticleOne ...
- java练习——多态与异常处理
1. 上面的程序运行结果是什么? 2. 你如何解释会得到这样的输出? parent = chlid; 所以child中的方法被赋予parent,所以用child方法输出了child的成员变量: ...
- Maven项目Update Project自动恢复为JRE1.5的问题
问题: 使用Eclipse建立Maven项目的时候,JDK默认为1.5在用户使用Config Build Path更新为最新JRE库比如1.8或者1.7的后,Maven项目显示JRE1.8 or 1. ...
- SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID
如题: SDK location not found. Define location with sdk.dir in the local.properties file or with an AND ...
- Nodejs-异步操作
1.阻塞 console.time('main');//代码计时器 //不断循环阻塞了代码的执行 for(var i=0;i<10000000;i++){ } console.timeEnd(' ...
- 剑指Offer - 九度1503 - 二叉搜索树与双向链表
剑指Offer - 九度1503 - 二叉搜索树与双向链表2014-02-05 23:39 题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树 ...
- Hastable和Dictionary以及ArrayList和(List,LinkedList,数组)的区别
Hastable和Dictionary的区别:(键值对) 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2:多线程程序中推荐使用 Hashtabl ...
- linux shell 总结 (整理)
ls /usr/bin/ info #路径操作 dirname basename #“”和‘’与 ` ` 在shell变量中的区别 “ ” 允许通过$符引用其他变量 ‘’禁止引用其他变量符,视为普通字 ...
- (原)Unreal渲染模块 管线 - 程序和场景查询
@author: 白袍小道 查看随意,转载随缘 第一部分: 这里主要关心加速算法,和该阶段相关的UE模块的结构和组件的处理. What-HOW-Why-HOW-What(嘿嘿,老规矩) 1.渲 ...