zoukankan      html  css  js  c++  java
  • 150. 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

    链接: http://leetcode.com/problems/evaluate-reverse-polish-notation/

    题解:

    求解逆波兰式。第一次听这个还是很久以前从老殷的口中。 典型的栈应用。计算结果然后push回栈里就可以了。ac的代码很简单,其实还应该加入很多检查,比如pop之前检查stack是否为空, parseInt之前检查是否可以被parse - 写一个tryParseInt的boolean方法。以后要继续注意细节。

    Time Complexity - O(n),Space Complexity - O(n)

    public class Solution {
        public int evalRPN(String[] tokens) {
            if(tokens == null || tokens.length == 0)
                return 0;
            Stack<Integer> stack = new Stack<>();
            
            for(int i = 0; i < tokens.length; i++) {
                String token = tokens[i];
                if(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                    int post = stack.pop();
                    int prev = stack.pop();
                    if(token.equals("+"))
                        stack.push(prev + post);
                    else if (token.equals("-"))
                        stack.push(prev - post);
                    else if (token.equals("*"))
                        stack.push(prev * post);
                    else
                        if(post == 0)
                            return 0;
                        else
                            stack.push(prev / post);
                } else
                    stack.push(Integer.parseInt(token));
            }
            
            return stack.pop();
        }
    }

    题外话: 第一遍刷题基本完成了一遍,后面就是新世界了。完成第一遍之后还要有第2,3,4,5遍,以及要补充许多其他知识,学习Python和JavaScript。努力吧。

    二刷:

    方法和一刷一样,就是使用一个Stack<Integer>来保存临时结果。在token等于数字的话直接push入栈。 在token等于符号的时候分别进行计算,之后再把结果push入栈。

    要注意计算的时候 parseInt返回的是int, valueOf返回的是Integer,这之间有个auto boxing 和unboxing。 假如有exception的话可以加try catch来throw new Runtime Exception。

    更好的写法是用一个switch语句,并且用int[] 数组来cache之前的结果。

    Java:

    Time Complexity - O(n),Space Complexity - O(n)

    public class Solution {
        public int evalRPN(String[] tokens) {
            if (tokens == null || tokens.length == 0) return 0;
            Stack<Integer> stack = new Stack<>();
            for (String token : tokens) {
                if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                    int num2 = stack.pop();
                    int num1 = stack.pop();
                    if (token.equals("+")) stack.push(num1 + num2);
                    else if (token.equals("-")) stack.push(num1 - num2);
                    else if (token.equals("*")) stack.push(num1 * num2);
                    else if (token.equals("/")) stack.push(num1 / num2);
                } else {
                    stack.push(Integer.valueOf(token));
                }
            }
            return stack.pop();
        }
    }

    Reference:

    https://leetcode.com/discuss/6266/java-accepted-code-stack-implementation

    https://leetcode.com/discuss/44526/accepted-clean-java-solution

    https://leetcode.com/discuss/89741/7ms-java-solution-beat-99-39%25

  • 相关阅读:
    <JZOJ5912>VanUSee
    <JZOJ5910>duliu
    <JZOJ5913>林下风气
    pytest学习笔记(二)
    试用saucelabs进行浏览器兼容性测试
    pytest学习笔记(一)
    SSM框架搭建,以及mybatis学习
    游戏2048的python实现
    使用svn在github上下载文件夹
    jenkins集成python的单元测试
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4489637.html
Copyright © 2011-2022 走看看