【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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- python按行读取apk中版本号、包名等信息
主要是读apk中manifest.xml中的信息. 读单一apk信息:见“文件”中“apkInfo.xml”.实际运行时,需要将后缀由“.xml”改为“.py". 批量自动获取apk软件信息 ...
- 读书报告之《改动代码的艺术》 (I)
<改动代码的艺术>,英文名<Working Effectively with Legacy Code>,中文翻译的文笔上绝对谈不上"艺术"二字.愧对艺术二字 ...
- Atitit。Time base gc 垃圾 资源 收集的原理与设计
Atitit.Time base gc 垃圾 资源 收集的原理与设计 1. MRC(MannulReference Counting手动 retain/release/autorelease语句1 2 ...
- centos7 firefox 安装flash
在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...
- Iwfu-GitHubclient使用
Git/GitHub介绍 GitHub是著名的项目托管平台,有关Git和GitHub的介绍參考以下链接: Git介绍 url=OlagjwbaAdSJ2pjckgJCLBYd-LFFTDBriRnLt ...
- 【JMeter4.0学习(十)】之JMeter函数简单运用以及结合正则表达式提取器
下面来简单的举个栗子: 首先,把函数和正则表达式提取器放在一块来介绍,如下所示: 1.结构完整展示,下面再一步一步创建添加: 2.添加线程组: 3.首先添加HTTP请求1 4.添加结果树后,运行后查看 ...
- ObjC消息机制
深入浅出ObjC之消息 罗朝辉(http://blog.csdn.net/kesalin) 在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC ...
- RocketMQ 4.3正式发布,支持分布式事务
冯嘉 作者 | 冯嘉 近日,Apache RocketMQ 4.3 版本宣布发布,此次发布不仅包括提升性能,减少内存使用等原有特性增强,还修复了部分社区提出的若干问题,更重要的是该版本开源了社 ...
- 嵌入式开发之simulation--- 双目移动dsp机器人
http://foundy.blog.163.com/blog/static/263383442014112391130207/
- Java多线程编程总结一:多线程基本概念
Java多线程编程总结一 – 初识多线程 进程.多进程.线程.多线程的概念 进程(process):CPU的执行路径.通俗的说就是系统中正在运行的程序.比如我们打开了浏览器.QQ等等,这些程序一旦被打 ...