【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 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- FormatFloat
http://www.delphibasics.co.uk/RTL.asp?Name=FormatFloat 1 function FormatFloat ( const Formatting : ...
- android dp 和 px 的相互转换
在开发中,可能须要动态设置控件的大小 比如为一个gridview设置宽度: LinearLayout.LayoutParams linearParams2 = (LinearLayout.Layout ...
- gopath基础概念
GOROOT golang安装路径. GOPATH 官方解释,请google.go工作环境中常常用到的一个很重要的环境变量(这种设计类似java).具体用途:go命令常常需要用到的,如go run,g ...
- ArcObject IFeature set_Shape()和Delete()报错
这样的问题主要是Ifeature实际在数据库里面不存在!可是通过IFeatureClass.getFeature()又可以得到! 详细操作流程: 首先是对要素进行删除,可是通过IFeatureClas ...
- mysql的UNIX_TIMESTAMP用法
UNIX_TIMESTAMP 一般是用于unix的时间戳. 例子: SELECT UNIX_TIMESTAMP("2016-07-11")-- 1468166400SELECT U ...
- 分享牛人就是的volatilekeyword
volatile作用 一个定义为volatile的变量是说这变量可能会被意想不到地改变.这样,编译器就不会去如果这个变量的值了. 精确地说就是.优化器在用到这个变量时必须每次都小心地又一次读取这个变量 ...
- sersync简介与测试报告
在分布式应用中会遇到一个问题,就是多个服务器间的文件如何能始终保持一致.一种经典的办法是将需要保持一致的文件存储在NFS上,这种方法虽然简单方便但却将本来多点的应用在文件存储上又变成了单点,这违背了分 ...
- nginx做反向代理proxy_pass,proxy_redirect的使用
大 | 中 | 小 今天用nginx作为trac的反代,发现一个问题,就是登入登出跳转的时候是白页,看了下网页相应内容,发现相应的location是空的.查了一下发现是只单纯用了proxy_pas ...
- Oracle PL/SQL 高级编程
1. 复合数据类型--记录类型 Ø 语法格式 type 类型名 is record ( 字段1 字段1类型 [not null]:=表达式1; 字段2 字段2类型 [not n ...
- 一种关键字搜索---edu.cn
比如要搜索知识点最小二乘,可以这样: 曲线拟合的最小二乘法 edu.cn 然后就一大片关于edu的相关链接,很多知名学校链接 http://www.bb.ustc.edu.cn/jpkc/xiaoji ...