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

题目大意:给定一个String数组,数据是一个后缀表达式,求这个表达式的值。

解题思路:用栈来保存操作数即可,遇到数就入栈,遇到操作符就从栈里取出两个元素运算然后入栈即可,最后栈里只剩一个元素就是结果。这个不带括号还容易一点,直接上代码。

  static Map<String, Integer> op = new HashMap<>();

    static {
op.put("+", 1);
op.put("-", 2);
op.put("*", 3);
op.put("/", 4);
} public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0) {
return 0;
}
Stack<Integer> nums = new Stack<>();
for (String s : tokens) {
if (op.get(s) != null) {
int fa = nums.pop();
int fb = nums.pop();
if (op.get(s) == 1) {
nums.push(fb + fa);
} else if (op.get(s) == 2) {
nums.push(fb - fa);
} else if (op.get(s) == 3) {
nums.push(fb * fa);
} else {
nums.push(fb / fa);
}
} else {
nums.push(Integer.valueOf(s));
}
}
return nums.peek();
}

Evaluate Reverse Polish Notation——LeetCode的更多相关文章

  1. 150. Evaluate Reverse Polish Notation - LeetCode

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

  2. Evaluate Reverse Polish Notation leetcode java

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  3. Evaluate Reverse Polish Notation --leetcode

    原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...

  4. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  5. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

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

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

  7. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  8. 【LeetCode练习题】Evaluate Reverse Polish Notation

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

  9. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

随机推荐

  1. 绝对炫的幻灯片插件-SKITTER

    绝对炫的幻灯片插件-SKITTER 所属分类:媒体-幻灯片和轮播图,图片展示,滑块和旋转 Includes code source // Styles <link href="css/ ...

  2. linux进程地址空间详解(转载)

    linux进程地址空间详解(转载) 在前面的<对一个程序在内存中的分析 >中很好的描述了程序在内存中的布局,这里对这个结果做些总结和实验验证.下面以Linux为例(实验结果显示window ...

  3. CSS控制鼠标滑过时的效果

    用css控制鼠标样式的语法如下:<span style="cursor:*">文本或其它页面元素</span>把 * 换成如下15个效果的一种: 下面是对这 ...

  4. 转载[POJ题型分类]

    北大ACM题分类 主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的 ...

  5. ASP生成新会员编号

    Function MakeUserCode OpenDB() Randomize dim getid_rs,getid set getid_rs=rsobj do while true getid=^ ...

  6. express不是内部命令解决办法

    安装nodejs后,安装express,cmd下命令:npm install express -g;之后敲入express -V会提示不是内部命令,是因为, 4.0版本中将命令工具分家出来了,所以我们 ...

  7. animationWithKeyPath键值对

    animationWithKeyPath键值对的方式来改变动画 <Jacky Shin:可以从这个网址查到哪些可以做为动画效果, 打开xcode帮助,搜索animatable propertie ...

  8. 查看sql语句执行时间/测试sql语句性能

    写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的 ...

  9. Pomelo实现最简单的通信-egret。

    昨天因为需要开始学习Pomelo 做H5游戏的服务端. 因为个人学习习惯,我从来不适合去跟着文档看.一般我直接是看下大概的API,但是Pomelo的API全部都是英文的. 昨天我就告诉自己用一下午时间 ...

  10. 挖潜无极限———数据挖掘技术与应用热点扫描[ZZ]

    “我们把世界看成数学,并且把你也看成数学”——用这句话来说明数据挖掘技术的复合性和应用的广泛性似乎再好不过.如今,虽然一些行业在应用这一技术上仍然缺乏足够的主动,但一个不能阻挡的趋势是:已经有越来越多 ...