[Leetcode] Evaluate Reverse Polish Notation (Java)
来源:互联网 发布:淘宝上买摩托车被骗了 编辑:程序博客网 时间:2024/05/22 17:26
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
计算后缀表达式的值
public class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack<Integer>(); for(int i=0;i<tokens.length;i++){String tmp = tokens[i];if(tmp.equals("+")||tmp.equals("-")||tmp.equals("*")||tmp.equals("/")){int b = stack.pop();int a = stack.pop();if(tmp.equals("+"))stack.push(a+b);else if(tmp.equals("-"))stack.push(a-b);else if (tmp.equals("*")) stack.push(a*b);elsestack.push(a/b);}else stack.push(Integer.parseInt(tmp));}return stack.pop(); }}
0 0
- [Leetcode] Evaluate Reverse Polish Notation (Java)
- Leetcode-Evaluate Reverse Polish Notation-java
- LeetCode:Evaluate Reverse Polish Notation (Java)
- Leetcode: Evaluate Reverse Polish Notation (Java)
- Evaluate Reverse Polish Notation leetcode java
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- QT的安装配置和使用总结(不定时更新)
- CUDA学习笔记之 CUDA存储器模型
- How to bounce Web server
- 我的shiro之旅: 八 shiro session 共享的进一步
- OSCache缓存框架介绍
- [Leetcode] Evaluate Reverse Polish Notation (Java)
- Makefile中指示符“include”、“-include”和“sinclude”的区别
- 二叉搜索树的插入和删除
- Ubuntu 13.04 eclipse启动菜单栏失效的解决方案
- MSM8X10调试AT2250添加在线调试模式
- struts ajax result param
- Warning: World-writable config file '/etc/my.cnf' is ignored
- Android面试题整理
- 数据结构实验之二叉树的建立与遍历