zoukankan      html  css  js  c++  java
  • 119. Pascal's Triangle II

    原题链接:https://leetcode.com/problems/pascals-triangle-ii/description/
    杨辉三角问题二,问题一虽然简单到我都实现了,问题二我也实现了,只是不太符合题目中所要求的空间复杂度罢了,后面参考了下别人的答案吧:

    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by clearbug on 2018/2/26.
     */
    public class Solution {
    
        public static void main(String[] args) {
            Solution s = new Solution();
            System.out.println(s.getRow3(0));
            System.out.println(s.getRow3(1));
            System.out.println(s.getRow3(2));
            System.out.println(s.getRow3(3));
            System.out.println(s.getRow3(4));
    
        }
    
        /**
         * 方法一:我自己的实现,空间复杂度貌似比要求的高了,效率也不高
         *
         * @param rowIndex
         * @return
         */
        public List<Integer> getRow(int rowIndex) {
            List<Integer> pre = new ArrayList<>();
            pre.add(1);
            if (rowIndex == 0) {
                return pre;
            }
            for (int i = 2; i <= rowIndex + 1; i++) {
                List<Integer> current = new ArrayList<>(i);
                for (int j = 0; j < i; j++) {
                    if ((j - 1) >= 0 && j < pre.size()) {
                        current.add(pre.get(j - 1) + pre.get(j));
                    }
                    if ((j - 1) >= 0 && j >= pre.size()) {
                        current.add(pre.get(j - 1));
                    }
                    if ((j - 1) < 0 && j < pre.size()) {
                        current.add(pre.get(j));
                    }
                }
                pre = current;
            }
            return pre;
        }
    
        /**
         * 方法二:别人提交的高效率的答案,但是我完全没看懂。。。
         * @param rowIndex
         * @return
         */
        public List<Integer> getRow2(int rowIndex) {
            List<Integer> row = new ArrayList<>();
    
            if (rowIndex < 0) {
                return row;
            }
            row.add(1);
    
            int n = rowIndex;
            int k = 1;
            long val = n / k;
    
            while (k <= rowIndex) {
                row.add((int) val);
                n--;
                k++;
    
                val = val * n;
                val = val / k;
            }
    
            return row;
        }
    
        /**
         * 方法三:优化空间复杂度,讨论区别人的答案,确实不错哦,这个比方法二好理解
         *
         * @param rowIndex
         * @return
         */
        public List<Integer> getRow3(int rowIndex) {
            List<Integer> row = new ArrayList<>(rowIndex + 1);
            if (rowIndex < 0) {
                return row;
            }
    
            for (int i = 0; i < rowIndex + 1; i++) {
                row.add(0, 1);
                for (int j = 1; j < row.size() - 1; j++) {
                    row.set(j, row.get(j) + row.get(j + 1));
                }
            }
    
            return row;
        }
    }
    
  • 相关阅读:
    能ping通Linux但是ssh连不上问题解决方法
    php遍历目录与文件夹的多种方法详解
    Apache与Nginx的优缺点比较
    Apache查看连接数和限制当前的连接数
    【MySql】性能优化之分析命令
    PHP实现各种经典算法
    301、404、200、304等HTTP状态
    常用服务器资源地址集合
    关于WAMP的apache 人多了就访问非常卡的问题解决方法
    HTML基础
  • 原文地址:https://www.cnblogs.com/optor/p/8586201.html
Copyright © 2011-2022 走看看