LeetCode:Evaluate Reverse Polish Notation
2016-06-16 11:52
441 查看
Evaluate Reverse Polish Notation
Total Accepted: 66669 TotalSubmissions: 278014 Difficulty: Medium
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
Subscribe to see which companies asked this question
Hide Tags
Stack
Hide Similar Problems
(H) Basic Calculator (H)
Expression Add Operators
思路:
逆波兰表达式。
java code:
public class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack<>(); int ans = 0; int x, y; for(String t : tokens) { switch(t){ case "+": stack.push(stack.pop() + stack.pop()); break; case "-": x = stack.pop(); y = stack.pop(); stack.push(y - x); break; case "*": stack.push(stack.pop() * stack.pop()); break; case "/": x = stack.pop(); y = stack.pop(); stack.push(y / x); break; default: stack.push(Integer.parseInt(t)); } } return stack.pop(); } }
相关文章推荐
- lua加载动态库缺乏相应的系统库
- lua防止访问不存在的全局变量
- ios waxpatch lua语法
- lua常见异常 USE " cc.exports. = value " INSTEAD OF SET GLOBAL VARIABLE"
- How do you evaluate music?
- Android和Luaj之间互相传值的问题
- lua 闭包
- Metatable 对象默认的操作方式
- luajit 2.1beta2 bytecode windows mac 下32 64编译文件
- Lua学习笔记2:Windows7下使用VS2008搭建Lua开发环境(二)
- Lua学习笔记1:Windows7下使用VS2008搭建Lua开发环境(一)
- 用vc 编译 luajit 动态库dll 与 静态库lib
- toLua简单使用
- PowerDesigner160_Evaluation导出oralce数据模型
- eclipse Error处理:Invalid arguments ' Candidates are: void luaL_openlibs(*)'
- Lua 语法基础
- openresty的安装与luarocks等插件的安装配置
- [ES7] Descorator: evaluated & call order
- Lua面向对象之类和继承
- Lua面向对象之类和继承浅析