Java for LeetCode 150 Evaluate Reverse Polish Notation
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
解题思路:
计算逆波兰表达式,Stack的经典应用,JAVA实现如下:
public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0)
return 0;
Stack<Integer> stack = new Stack<Integer>();
for (String str : tokens) {
if (str.length() > 1 || Character.isDigit(str.charAt(0)))
stack.push(Integer.parseInt(str));
else {
int a = stack.pop();
int b = stack.pop();
if (str.equals("+"))
stack.push(b + a);
else if (str.equals("-"))
stack.push(b - a);
else if (str.equals("*"))
stack.push(b * a);
else if (str.equals("/"))
stack.push(b / a);
}
}
return stack.pop();
}
Java for LeetCode 150 Evaluate Reverse Polish Notation的更多相关文章
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- 【Matplotlib】设置刻度(1)
刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(*args, **kwargs) 获取或者设置当前刻度位置和文本的 ...
- javaScript基础练习题-下拉框制作(神奇的代码)
http://www.oschina.net/code/snippet_12_46548#66319 http://www.codeproject.com/Tips/890021/Advanced-C ...
- poj 3233 矩阵快速幂+YY
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
- UVA 11527 Unique Snowflakes
用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- python scrapy 获取华为应用市场APP评论数据
scrapy入门 四步: 1. 创建一个新的Scrapy Project 2. 定义你需要从网页中提取的元素Item 3. 实现一个Spider类,通过接口完成爬取URL和提取Item的功能 4. 实 ...
- android button 函数调用栈
Button button=(Button) findViewById(R.id.button);button.setOnClickListener(new Button.OnClickListene ...
- centOS下yum安装配置samba
centOS下yum安装配置samba 2010-03-29 15:46:00 标签:samba yum centOS 安装 休闲 注意:本文的原则是只将文件共享应用于内网服务器,并让将要被共享的目 ...
- Linux 安装Rsync和配置
1.检查rsync 是否已经安装 [root@test home]# rpm -qa|grep rsync 若已经安装,则使用rpm -e 命令卸载. [root@test home]#rpm -e ...
- js获取单选框里面的值
rt,如果想获取单选框里面的值,该如何获取呢. <script> window.onload = function(){ //通过名字获取 getElementsByName //var ...