【leetcode刷题笔记】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
题解:典型的栈的应用——计算后缀表达式。
思路很简单:
遇到符号就从栈里面弹出来两个元素进行相应的计算后得到的结果重新放回栈中
遇到数字就直接压入栈中。
代码如下:
public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<Integer>();
for(String x:tokens){
if(x.equals("+"))
s.push(s.pop()+s.pop());
else if(x.equals("-")){
int b = s.pop();
int a = s.pop();
s.push(a-b);
}
else if(x.equals("*"))
s.push(s.pop()*s.pop());
else if(x.equals("/")){
int b = s.pop();
int a = s.pop();
s.push(a/b);
}
else{
s.push(Integer.parseInt(x));
}
}
return s.pop();
}
}
特别注意的地方有两点:
1.将一个String转换成Integer用Integer.parseInt()函数
2.开始我用的是“==”来比较两个字符串是否相等,后来发现“==”其实比较的是字符串的地址是否相等,如果要比较字符串的内容是否相等要用s.equals()函数。
不过很奇怪的一点是在自己电脑的eclipse上面用“==”居然也能够算出正确的值。
【leetcode刷题笔记】Evaluate Reverse Polish Notation的更多相关文章
- 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 +, ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- 【leetcode刷题笔记】Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 【leetcode刷题笔记】Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【leetcode刷题笔记】Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- 如何在 Linux 下大量屏蔽恶意 IP 地址
很多情况下,你可能需要在Linux下屏蔽IP地址.比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰.或者当你在运行P2P软件时.你可能想要过滤反P2P活动的网络链接.如果你是一名系统管 ...
- Excel2007制作直方图和正态分布曲线图
对同一维度的数据分析数据分布范围及分布趋势,要通过制作直方图和正态分布曲线图体现. 例如:已知所有员工的日收入,分析员工收入分布情况(51.7,50.6,57.9,56.9,56.7,56.7,55. ...
- window.location网页URL信息
window.location属性 描述 hash 设置或获取 href 属性中在井号“#”后面的分段. host 设置或获取 location 或 URL 的 hostname 和 port 号码. ...
- POJ1328 Radar Installation 【贪心·区间选点】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54593 Accepted: 12 ...
- Active Directory的基本概念
前言 本文是面对准备加入Active Directory编程的初学者的一份文章,主要是讲解Active Directory(活动目录)的一些概念和相关知识.这篇文章本来是不想写下来的,因为概念性内容的 ...
- Android Studio导入eclipse工程(引用多个其它工程)
eclipse工程向android studio 迁移过程中需要到编译错误: eclipse工程的结构比较复杂,引用了其它的工程,在迁移的过程中遇到了错误. @ViewInject(R.id.edit ...
- 安装Struts2 类库
现在,如果一切正常,那么你可以继续设置您的Struts 2框架.以下是简单的步骤,下载并安装在机器上Struts2. 请选择是否要安装Hibernate在Windows或Unix,然后继续进行下一个步 ...
- 多媒体开发之--- rtsp 中的H264 编码+打包+解码相关知识es、pes、ts...
1)ES流(Elementary Stream): 也叫基本码流,包含视频.音频或数据的连续码流. 2)PES流(Packet Elementary Stream): 也叫打包的基本码流, 是将基本的 ...
- systemctl使用说明
# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...
- Android 浏览器文本垂直居中问题
问题描述 在开发中,我们常使用 line-height 属性来实现文本的垂直居中,但是在安卓浏览器渲染中有一个常见的问题,就是对于小于12px的字体使用 line-height 属性进行垂直居中的时候 ...