题目链接

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

法一: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. 笔记-python-module-logging.循环日志、多进程日志

    笔记-python-module-logging.循环日志.多进程日志 1.      logging循环日志 循环日志分为按大小切分和按时间切分,对应实现类如下. 1.1.  RotatingFil ...

  2. gettid 和pthread_self的区别

    转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...

  3. runloop和线程有什么关系?

    每条线程都有唯一的一个RunLoop对象与之对应的 主线程的RunLoop是自动创建并启动 子线程的RunLoop需要手动启动 子线程的RunLoop创建步骤如下: 获得RunLoop对象后要调用ru ...

  4. 使用自己的类来作为hashtable的主键

    import java.util.*; class Counter { } class Groundhog2 { int ghNumber; Groundhog2(int n) { ghNumber ...

  5. 《数据结构与算法分析:C语言描述》复习——第三章“线性表、栈和队列”——双向链表

    2014.06.14 20:17 简介: 双向链表是LRU Cache中要用到的基本结构,每个链表节点左右分别指向上一个和下一个节点,能够自由地左右遍历. 图示: 实现: // My implemen ...

  6. python学习笔记十七:base64及md5编码

    一.Python Base64编码 Python中进行Base64编码和解码要用base64模块,代码示例: #-*- coding: utf-8 -*- import base64 str = 'c ...

  7. sql 表数据转移另一张表

     if not exists(select * from syscolumns where id=object_id('REMOTEDETECTION_2018')) begin SELECT * I ...

  8. 【Linux】wc :字数统计命令

    wc :(Word Count) 统计每个传入文件中行数.词数与字节数 $ wc py_this # 三个数字分别对应行数.词数和字节数 21 144 857 py_this $ wc py_this ...

  9. VB.NET视频总结——后续篇

    上篇基础总结简单介绍了前几个单元的主要内容和理解的思路,这篇介绍后几个单元的内容,主要介绍了应用程式的设计与部署方面的内容. 首先,第十一单元讲的是应用程式设计的基础,主要讲解了元件的相关知识.应用程 ...

  10. 【Python】- pytharm 中import时无法识别自己写的程序

    右键点击自己的工作空间,找下面的Mark Directory as(将目录标记为) 选择Source Root,就可以解决上面的问题了,如图