150. Evaluate 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[] s) { Stack<Integer> stack1=new Stack<>();
for(int i=0;i<s.length;i++)
{
if(s[i].equals("+"))
{
int a=stack1.pop();
int b=stack1.pop();
stack1.push(a+b);
}
else if(s[i].equals("-"))
{
int a=stack1.pop();
int b=stack1.pop();
stack1.push(b-a);
}
else if(s[i].equals("*"))
{
int a=stack1.pop();
int b=stack1.pop();
stack1.push(a*b);
}
else if(s[i].equals("/"))
{
int a=stack1.pop();
int b=stack1.pop();
stack1.push(b/a);
}
else
{
try{stack1.push(Integer.parseInt(s[i].trim()));}
catch(NumberFormatException e){} } }
return stack1.peek();
}
}
150. Evaluate Reverse Polish Notation的更多相关文章
- 【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 ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for 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 ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 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 +, -, ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- 优化MYSQL数据库的方法
1.选取最适用的字段属性,尽可能减少定义字段长度,尽量把字段设置NOT NULL,例如'省份,性别',最好设置为ENUM2.使用连接(JOIN)来代替子查询: a.删除没有任何订单客户:DELETE ...
- HDU 3642 扫描线(立方体体积并)
Get The Treasury Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ubuntu连接Android调试
从这周开始尝试Android开发,记下点滴. 安装JDK.下载ADT不说,连接手机调试的时候出错,一堆问号??????????.网上一查,属于典型错误.试下来,有几步比较关键,容易忽视: 1.我机器上 ...
- C# Process打开程序并移动窗口到指定位置
process.start只是按指定的参数来运行一个程序,而这个程序自己运行起来是什么样子的就不是Process所能处理的了,不过当程序运行起来后倒是可以通过Process的MainWindowHan ...
- jpcap
1.System.out.println( System.getProperty("java.library.path")); 2.将jpcap.dll放到上边打印的路径中
- 图解傅里叶变换(so easy)
话不多说先上两个GIF图. 第一个动画和第二个动画其实都是对时域的周期矩形形波(近似看成矩形波,并不是严格意义的矩形方波)进行傅里叶变换分析. 对于第一个图形来说,它侧重展示变换的本质之一:叠加性,每 ...
- 自定义圆的半径layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- Xp 消息队列的使用
1.安装消息队列3.0: 控制面板/添加删除程序/添加window组件/找到消息队列/选择->详细信息->MSMQ HTTP支持. 注意:如果计算机没有连接到域需要去掉Active Dir ...
- sql游标的使用入门
游标的理解: 游标其实可以理解成一个定义在特定数据集上的指针,我们可以控制这个指针遍历数据集,或者仅仅是指向特定的行,所以游标是定义在以Select开始的数据集上的 普通的sql语句是面向集合的,游标 ...
- Thread IsBcakgroud
C#中,Thread类有一个IsBackground 的属性.MSDN上对它的解释是:获取或设置一个值,该值指示某个线程是否为后台线程.个人感觉这样的解释等于没有解释. .Net中的线程,可以分为后台 ...