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 +, -, ...
随机推荐
- Echarts 地图控件tooltip多行显示
直接上代码 var o = { "tooltip": { trigger: 'item', "formatter": function (params) { v ...
- 开通了cnblogs
受够了百度空间,换个地方,或许会更好. 以后有机会会将百度空间你的文章搬过来的.
- OpenLayers简单介绍以及简单实例
OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...
- Jquery实现的Tabs标签页
效果图: HTML: <div class="tabs"> <ul id="tabs"> <li class="tab- ...
- 通过使用ScriptManager.RegisterStartupScript,呈现后台多次使用alert方法
在前台HTML中加入alert或者confirm,相信大家已经非常熟悉并且经常使用: <div onclick="alert('hello')">按钮1</div ...
- greenDao 3.0基础
引入greenDao3.0 首先在project的gradle文件中引入greenDAO插件 dependencies { classpath 'com.android.tools.bui ...
- 2013年8月份第1周51Aspx源码发布详情
校企工作室OA源码 2013-8-9 [VS2010]源码描述:主要模块及系统管理功能说明:一.考勤功能模块:考勤分成三个功能,显示签到功能,查询功能,管理功能.1.签到功能分析:在签到功能中,我们 ...
- C# 展开和折叠代码的快捷键(总结)
C# 展开和折叠代码的快捷键 VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + ...
- hdu 2058
PS:TLE了N次...虽然结果对了...后来看了公式才知道要枚举项数才行... 代码: #include "stdio.h"#include "math.h" ...
- 进行以上Java编译的时候,出现unmappable character for encoding GBK。
public class Exerc02{ public static void main(String args []){ char c = '中国人'; System.out.pingtln(c) ...