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. 关于WINDOWS命令

    1. Windows netstat 查看端口.进程占用 netstat -aon —  显示全部进程 2. 查看进程命令 tasklist — 显示全部进程 taskkill — 关闭至少一个系统进 ...

  2. 在eclipse下面搭建Clojure开发运行环境

    打开eclipse,点击菜单栏“help->Install New Software...", 然后,点击”add“, 在Location处输入 http://ccw.cgrand.n ...

  3. Ubuntu Server 14.04 下root无法ssh登陆

    今天安装了Ubuntu Server 14.04   在终端配置了root密码后,使用SecureCRT和putty竟然不能ssh登陆,SecureCRT一直提示密码不对,但是可以肯定输入的密码100 ...

  4. Angularjs总结(六) 上传附件

    所用插件:angular-file-upload 这个插件用到的几个指令:nv-file-select(点击选择).uploader(用于绑定控制器中新建的uploader对象) HTML: < ...

  5. What and where are the stack and heap?

    The stack is the memory set aside as scratch space for a thread of execution. When a function is cal ...

  6. Codeforces 474F - Ant colony

    注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...

  7. java的动态绑定与双分派(规避instanceof)

    1. 动态绑定的概念 指程执行期间(而不是在编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法 . 例如: package org.demo.clone.demo; public c ...

  8. 页面插入Flash方式

    法一 <!-- 播放Flash动画代码 --> <div class="logoFlash"> <object classid="clsid ...

  9. wpf样式绑定 行为绑定 事件关联 路由事件实例

    代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化 首先三个窗体不用 ...

  10. 简单实现tab标签页切换

    常见面试题: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...