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 +, -, ...
随机推荐
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
#include<cstdio> #include<iostream> using namespace std; ][],n,m,fa[],st[]; ]; bool isro ...
- C++定义构造函数必须使用初始化列表的场合
明其理,而知其然也. 先给理论.1. 初始化 != 赋值. a.初始化代表为变量分配内存. 变量在其定义处被编译器初始化(编译时). 在函数中, 函数参数初始化发生在函数调用时(运行时). b.赋值代 ...
- 一模 (5) day2
第一题: 题目大意:使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? n<=2*10^9 解题过程: 1.以前看到过这题了,一个数x的位数=(int)lg(x)+1 换一下底就是 ...
- dancing link 学习资源导航+心得
dancing link简直是求解数独的神器,NOIP2009最后一题靶形数独,DFS 各种改变搜索顺序 都没法过,最后还是用了卡时过得.用dancing link写,秒杀所有数据,总时间才400ms ...
- 【NOIP2015】提高组D1 解题报告
P1978神奇的幻方 Accepted 描述 幻方是一种很神奇的 N ∗ N 矩阵:它由数字 1,2,3, … … , N ∗ N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N 为奇数时 ...
- RM报表的选项 注册表位置
HKCU\Software\WHF SoftWare\Report Machine\RMReport\Form\RMDesignerForm\ 设计器-工具-选项的设置 HKCU\Software\W ...
- xlistview的(java)
package com.bwie.xlistviews; import java.text.SimpleDateFormat;import java.util.Date; import com.bwi ...
- 提示框alertmsg
初始化: 1.Data属性:DOM添加属性data-toggle="alertmsg",并定义type及msg参数 示例代码: <button type="butt ...
- 记录一些容易忘记的属性 -- UIKeyboard
//UIKeyboardWillShowNotification这个通知在软键盘弹出时由系统发送 //UIKeyboardWillShowNotification 通知:键盘将要显示的通知 ...
- 红帽中出现”This system is not registered with RHN”的解决方案
原因是你的linux没有在红帽网络上注册,所以无法下载上面的软件包,替代方案可以使用centos. 下面介绍下使用centos 的流程 1.卸载rhel的默认安装的yum包查看yum包rpm -qa| ...