题目链接

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

法一: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. Echarts 背景渐变柱状图

    var dom = document.getElementById("container"); var myChart1 = echarts.init(dom); var app ...

  2. 【UE4】二十四、UE4内部版本引擎和官方版本引擎版本保持兼容的方法

    内部使用的引擎和官方正式发布的引擎版本号不一致,这种情况会导致一些插件由于版本不一致无法使用,有其是在没有插件源码的情况下.解决方法为 修改Engine\Source\Runtime\Launch\R ...

  3. Thread-local storage (TLS)

    线程存储原理:为变量在每一个现存的线程里分配一个实例,需要处理器支持,并不是所有都支持!支持全局的,静态的变量,但不支持局部变量. 关键字 __thread   __thread int i;   e ...

  4. 用JAX-WS在Tomcat中发布WebService

    JDK中已经内置了Webservice发布,不过要用Tomcat等Web服务器发布WebService,还需要用第三方Webservice框架.Axis2和CXF是目前最流行的Webservice框架 ...

  5. day06_06 字典操作01

    1.0 字典操作 dic1 = {'name':'alex'} dic1['age'] = 18 print(dic1) #>>>{'age': 18, 'name': 'alex' ...

  6. python3.x 安装命令

    在root下执行下面的命令即可: sudo apt-get install python3-dev build-essential libssl-dev libffi-dev libxml2 libx ...

  7. [0] OpenCV_Notes - 琐碎

    CV_8UC1,CV_8UC2,CV_8UC3等意思 一般的图像文件格式使用的是 Unsigned 8bits,CvMat矩阵对应的参数类型就是CV_8UC1,CV_8UC2,CV_8UC3.最后的C ...

  8. 【Python】重定向 Stream 到文件

    Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...

  9. Elasticsearch相关度评分_score

    相关度评分 _score 的目的 是为了将当前查询的结果进行排序,比较不同查询结果的相关度评分没有太大意义. _score的计算方式 score(q,d) = # score(q,d) 是文档 d 与 ...

  10. 转: jsp之c标签

    http://www.gbsou.com/2009/10/12/1028.htmljsp标签之c标签 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测 ...