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 ...
随机推荐
- Android 游标
静下心来,学一下Android的数据库连接. 1.直接从getReadableDatabase()与getWritableDatabase()入手. --getReadableDatabase( ...
- 【转】正则表达式速查表(http://www.jb51.net/shouce/jquery1.82/regexp.html)
正则表达式速查表 字符 描述 \ 将下一个字符标记为一个特殊字符.或一个原义字符.或一个向后引用.或一个八进制转义符.例如,“n”匹配字符“n”.“\n”匹配一个换行符.串行“\\”匹配“\”而“\( ...
- OpenCV学习笔记(十一) 轮廓操作
在图像中寻找轮廓 首先利用Canny算子检测图像的边缘,再利用Canny算子的输出作为 寻找轮廓函数 findContours 的输入.最后用函数 drawContours 画出轮廓.边界Counto ...
- MySQL之查询性能优化(二)
查询执行的基础 当希望MySQL能够以更高的性能运行查询时,最好的办法就是弄清楚MySQL是如何优化和执行查询的.MySQL执行一个查询的过程,根据图1-1,我们可以看到当向MySQL发送一个请求时, ...
- echo shell commands as they are executed
http://stackoverflow.com/questions/2853803/in-a-shell-script-echo-shell-commands-as-they-are-execute ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- 剑指Offer - 九度1389 - 变态跳台阶
剑指Offer - 九度1389 - 变态跳台阶2013-11-24 04:20 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳 ...
- DOS程序员手册(四)
5.4打印机功能 打印机是能够直接控制的输出设备之外的唯一的重要输出设备.它们的功能比屏幕 107页 功能要简单得多,因为它们只涉及字符输出,并最小程度地与打印机的输入有关. 输出给打印机的最简单的方 ...
- webdriver--设置元素等待
sleep():脚本执行到某一位置时“睡一会”,再继续执行:参数的单位是s:sleep方法由python的time模块提供,有两种引入和使用方式 import time time.sleep(5) f ...
- 5.0 Genymotion安装以及基础使用
后续考虑到python+appium多设备并发执行,需要多台手机,所以这里就直接更新一个jenymotion,后续多设备执行直接用真机+模拟器操作!Genymotion第一步:百度搜索[Genymot ...