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. SpringMVC中Controller和RestController

    项目中的@Controller下有的是返回String类型的(比如getAllBook),有的是void的,当然,String类型是转发的页面,在void中用的是pringwrite,我今天想做一件事 ...

  2. BZOJ3720 Gty的妹子树

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  3. map 几种遍历方法

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  4. $root knockout

    http://www.cnblogs.com/rohelm/p/3209757.html 以列表方式呈现数据  处理以数组形式储存的多条数据,要先认识foreach.在ViewModel定义一个Jav ...

  5. jquery发送ajax请求返回数据格式

    jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. 1.html格式的数据 "<div class='comment ...

  6. 大数据分析与机器学习领域Python兵器谱

    http://www.thebigdata.cn/JieJueFangAn/13317.html 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/ ...

  7. 清空file input框

    测试环境:OS --> winXPBrowsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, ...

  8. Linux创建线程

    #include"stdio.h" #include"pthread.h" #include"unistd.h" ; void *creat ...

  9. 如何建立批处理文件(.bat或.cmd)

    如何建立批处理文件(.bat或.cmd) 建立批处理文件 批处理文件就是把多个dos命令放在一起. 批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令 ...

  10. 如果jsp提交到action为空指针的话

    很严重的一点:表单<form>有没有添加一个method="post",如果表单的这个没有写,肯定是空指针, 哎,被这个坑爹的代码,查了好久,特此记录下