题目链接

题目大意:计算逆波兰表达式的值。

法一: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---逆波兰式求值的更多相关文章

  1. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

    求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如:  ["2", "1&quo ...

  2. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  3. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  4. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  5. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  6. Evaluate Reverse Polish Notation(逆波兰式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 150. Evaluate Reverse Polish Notation(逆波兰表达式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. Java Evaluate Reverse Polish Notation(逆波兰式)

    表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...

  10. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

随机推荐

  1. HTML5 canvas 圆盘抽奖

    使用html5 canvas 绘制的圆盘抽奖程序 效果图: 贴上全部代码:  1 <!DOCTYPE html> <html> <head> <meta ch ...

  2. PHP.16-PDO

    PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库的特性作为标准扩展功能. 注意利用 PDO 扩展自身并不能实现任何数 ...

  3. I2C中24C02从地址设置

    从设备地址 首先,先看一下AT24C02的芯片资料,我们会发现AT24C02有三个地址A0,A1,A2.同时,我们会在资料的Device Address介绍发现I2C器件一共有七位地址码,还有一位是读 ...

  4. React-router4简约教程

    React-router4简约教程 教程       webEmmet 17年10月   React-router和React-router-dom的选择 很多刚使用react的同学在接触到react ...

  5. 《Cracking the Coding Interview》——第5章:位操作——题目5

    2014-03-19 06:22 题目:将整数A变成整数B,每次只能变一个二进制位,要变多少次呢. 解法:异或,然后求‘1’的个数. 代码: // 5.5 Determine the number o ...

  6. 《Cracking the Coding Interview》——第3章:栈和队列——题目6

    2014-03-19 03:01 题目:给定一个栈,设计一个算法,在只使用栈操作的情况下将其排序.你可以额外用一个栈.排序完成后,最大元素在栈顶. 解法:我在草稿纸上试了试{1,4,2,3}之类的小例 ...

  7. java程序员笑不死的经历ส้้้้้้้้้

    ส้้้้้้้้้้ส้้้้้้้้้้ส้้้้้้้้้ 1.程序猿最烦两件事,第一件事是别人要求他给自己的代码写文档,第二件呢?是别人的程序没有留下文档. 2.宪法顶个球!中国的法律都是.t ...

  8. 聊聊、Git 常用命令

    创建本地仓库git initgit add .git commit -m "xxxxx"git remote add origin http://git.xxx.com/xxx.g ...

  9. EasyUi DataGrid 请求Url两次问题

    easyui datagrid 1.4 当total为0时,请求两次url问题 框架问题:需要在easyui文件后加修补补丁 /** * The Patch for jQuery EasyUI 1.4 ...

  10. Python数据分析-Pandas(Series与DataFrame)

    Pandas介绍: pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. Pandas的主要功能: 1)具备对其功能的数据结构DataFrame.Series 2)集成时间序 ...