lintcode--逆波兰表达式求值
来源:互联网 发布:网络整天不稳定 编辑:程序博客网 时间:2024/05/17 08:55
求逆波兰表达式的值。
在逆波兰表达法中,其有效的运算符号包括 +
, -
, *
,/
。每个运算对象可以是整数,也可以是另一个逆波兰计数表达。
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
/*思路:碰到不是+*-/就转换为整数压栈,碰到了就弹出两个数进行运算,得到的结果再压栈。最后压出最后的值*/public class Solution { public int evalRPN(String[] tokens) { Stack<Integer> s = new Stack<Integer>(); String operators = "+-*/"; for(String token : tokens){ if(!operators.contains(token)){ s.push(Integer.valueOf(token)); continue;//不能用break } int a = s.pop(); int b = s.pop(); if(token.equals("+")) { s.push(b + a); } else if(token.equals("-")) { s.push(b - a); } else if(token.equals("*")) { s.push(b * a); } else { s.push(b / a); } } return s.pop(); }}
- LintCode : 逆波兰表达式求值
- LintCode:逆波兰表达式求值
- Lintcode 逆波兰表达式求值
- lintcode--逆波兰表达式求值
- Lintcode 逆波兰表达式求值
- 逆波兰表达式求值-LintCode
- lintcode-逆波兰表达式求值-424
- LintCode 424-逆波兰表达式求值
- lintcode-逆波兰表达式求值-424
- LintCode:M-逆波兰表达式求值
- 【LintCode】Evaluate Reverse Polish Notation 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 逆波兰表达式求值
- 常用算法:递归,回溯
- unity3d给子物体添加MeshCollider
- Android知识积累
- 500. Keyboard Row
- Golang教程:(二十一)协程
- lintcode--逆波兰表达式求值
- POJ 2155 Matrix 树状数组
- nginx安装
- 关于电脑连不上WiFi,但可以连上网线的解决办法
- 这几天的总结
- openfire xmpp 通过presence service判断用户是否在线
- 【WebDriver】解决ElementNotVisibleException方法总结
- Memcached-1.4.4-14 For Win32 or Win64
- 【SQL两表关联级联】两张表关联,统计级联数量ORACLE