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的更多相关文章

  1. LeetCode(150) Evaluate Reverse Polish Notation

    题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...

  2. 力扣算法题—150. Evaluate Reverse Polish Notation

      Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,  ...

  3. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  4. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  5. 【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 ...

  6. 【leetcode刷题笔记】Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  7. 【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-> ...

  8. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  9. 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 ...

  10. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

随机推荐

  1. myeclipse8.6 git 安装学习

    只有公司有svn,回到宿舍无法访问,因此没法做迭代开发,因此最近学习了git,这样在公司了提交的代码,回到宿舍也可以继续开发,用了一天的时间才弄明白git的原理,智商有问题啊,下面说下具体步骤,个人已 ...

  2. nginx 使用ngx_cache_purge清除缓存

    location ~ ^/myclear(/.*) { allow 10.0.0.0/8; allow 10.28.100.0/24; allow 127.0.0.1; deny all;   pro ...

  3. SpringSecurity学习二----------实现自定义登录界面

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  4. macbook 下SDK Manager 的更新[即使Google被屏蔽了也无所谓]

    废话少说,直接说操作步骤: 1. 改动/etc/hosts文件: 右键单击Finder,选择前往目录,输入/private/etc/,敲回车键.将文件/etc/hosts复制到桌面(由于权限受限.无法 ...

  5. 模拟和数字低通滤波器的MATLAB实现

    低通滤波器参数:Fs=8000,fp=2500,fs=3500,Rp=1dB,As=30dB,其他滤波器可以通过与低通之间的映射关系实现. %%模拟滤波器 %巴特沃斯——滤波器设计 wp=2*pi*2 ...

  6. 在Ubuntu 16.04下安装 virtualbox 5.2

        sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" ...

  7. gulp配置,实例演示

    项目完成后的目录 我们所需要的插件为:gulp-minify-css gulp-concat gulp-uglify gulp-rename del 如下图所示,完成后的项目目录结构: 附加,获取pa ...

  8. IOS开发中的分享到邮件

    本篇和UIWebView的全屏截图,可以一起使用,先对UIWebView进行截图,然后分享到邮箱(当时做还有分享到微信.腾讯微博.新浪微博功能,这三个根据官方资料,比较容易实现,这里就不进行解说了). ...

  9. Linux下文件的堵塞与非堵塞对部分系统调用的影响

    1.基本概念 所谓的堵塞,即内核在对文件操作I/O系统调用时.假设条件不满足(可能须要产生I/O),则内核会将该进程挂起.非堵塞则是发现条件不满足就会马上返回. 此外须要注意的是非堵塞并非轮询.不然就 ...

  10. PHP 学习内容

    第一阶段: (PHP+MySQL核心编程) 面向对象编程 MySQL数据库, MySQL的优化细节. HTTP协议,http也是我们web开发的基石.对我们了解PHP底层机制有很大帮助,做到知其然,还 ...