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. UITableViewCell中的UILabel添加手势没有响应的解决方法

    有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势.但是如果按照常规的添加方法,发现所添加的手势并不能响应.以下为解决方法:将手势 ...

  2. Android中ProgressBar

    ProgressBar提供如下方法来操作进度: setProgress(int):设置进度完成的百分比; incrementProgressBy(int):设置进度条的进度的增加或减少,具体取决于参数 ...

  3. DASH简介及使用方法(FFmpeg, MP4Box)

    DASH 为什么选择DASH YouTube采用DASH!其网页端及移动端APP都使用了DASH.DASH的其他采用者包括:Netflix, Hulu, … 什么是DASH 一种服务端.客户端的流媒体 ...

  4. Javascript属性constructor/prototype的底层原理

    在Javascript语言中,constructor属性是专门为function而设计的,它存在于每个function的prototype属性中. 这个constructor保存了指向function ...

  5. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

  6. threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet(maven项目git后)

    maven项目git全新项目后启动服务出现的, 错误原因: 进入到tomcat的部署路径.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpw ...

  7. linux系统下实时监控进程以及定位杀死挂起的进程

    一.实时监测进程 [root@instance-3lm099to ~]# top top - :: up days, min, users, load average: 0.01, 0.05, 0.0 ...

  8. Debug与Trace工具类的应用

    在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...

  9. C# 为枚举创建新方法

    可以使用扩展方法添加特定于某个特定枚举类型的功能. 示例在下面的示例中,Grades 枚举表示学生可能在班里收到的字母等级分.该示例将一个名为 Passing 的扩展方法添加到 Grades 类型中, ...

  10. 使用Erlang实现简单的排序算法:快速排序,冒泡排序,插入排序

    [排序算法] -module(sort). -compile(export_all). %%快速排序 qsort([]) -> []; qsort([Pivot|T]) -> qsort( ...