Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
解题思路:

计算逆波兰表达式,Stack的经典应用,JAVA实现如下:

	public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0)
return 0;
Stack<Integer> stack = new Stack<Integer>();
for (String str : tokens) {
if (str.length() > 1 || Character.isDigit(str.charAt(0)))
stack.push(Integer.parseInt(str));
else {
int a = stack.pop();
int b = stack.pop();
if (str.equals("+"))
stack.push(b + a);
else if (str.equals("-"))
stack.push(b - a);
else if (str.equals("*"))
stack.push(b * a);
else if (str.equals("/"))
stack.push(b / a);
}
}
return stack.pop();
}

Java for LeetCode 150 Evaluate Reverse Polish Notation的更多相关文章

  1. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  2. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

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

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

  4. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  5. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  7. 150. Evaluate Reverse Polish Notation - LeetCode

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

  8. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. JAVA运行java程序

    程序代码: public class f{ public static void main(String[] args){ String foo1 = args[1]; String foo2 = a ...

  2. 【转】mac os x系统上Android开发环境的搭建

    由于Google官方已经不提供Adt-Bundle下载了,主推AndroidStudio.可以从这个链接下载http://www.androiddevtools.cn.上面不光有adt-bundle, ...

  3. list 内部方法

    代码 #list内部方法 l=['a','9','c','a','3','7'] print(dir(l)) l.append('v') print(l)#append(self, p_object) ...

  4. 【poj1201】 Intervals

    http://poj.org/problem?id=1201 (题目链接) 题意 给出n个区间${[ai,bi]}$,要求选出尽可能少的数,使得每个区间i中至少存在${c[i]}$个数. Soluti ...

  5. 修改eclipse/MyEclipse中包的显示结构为树形

    在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图:

  6. IF IE

    1. <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->2. <!--[if IE]> 所有的IE可识别 & ...

  7. linux 定时 svn 代码更新,配置文件不修改

    普通参数: 普通参数为正常的传参数:  例子:  f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束.   例子:  ...

  8. [pdf.js]预览pdf时,中文名称乱码的问题

    在项目中使用了pdf.js的方式预览pdf,但针对中文名称的时候会出现乱码,导致找不到该文件而出现错误. 解决办法 <script src="viewer.js" chars ...

  9. 操作sqlserver数据库常用的三个方法

    1. ADO.NET -> 连接字符串,常用的两种方式: server=计算机名或ip\实例名;database=数据库名;uid=sa;pwd=密码; server=计算机名或ip\实例名;d ...

  10. CrtCtl (客户端认证的证书、私钥)的控制

    crt (证书文件) 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 客户端认证的证书.私钥. 中文名 crt 性    质 证书文件 类    型 客户端认证的证书. ...