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

    题目:

    Given numRows, generate the first numRows of Pascal's triangle.

    For example, given numRows = 5,
    Return

    [
         [1],
        [1,1],
       [1,2,1],
      [1,3,3,1],
     [1,4,6,4,1]
    ]
    

    链接:  http://leetcode.com/problems/pascals-triangle/

    题解:

    帕斯卡三角形,按照题意编写就可以了。现在每次写完以后都会去翻一翻discussion,看一看更好的写法。

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

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            if(numRows <= 0)
                return res;
            ArrayList<Integer> list = new ArrayList<>();
            list.add(1);
            res.add(list);
            
            for(int i = 1; i < numRows; i++) {
                ArrayList<Integer> temp = new ArrayList<>();
                temp.add(1);
                
                for(int j = 1; j < res.get(i - 1).size(); j++)
                    temp.add(res.get(i - 1).get(j) + res.get(i - 1).get(j - 1));
                    
                temp.add(1);
                res.add(temp);
            }
            
            return res;
        }
    }

    Update:

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            ArrayList<Integer> list = new ArrayList<>();
            
            for(int i = 0; i < numRows; i++) {
                list.add(0, 1);
                
                for(int j = 1; j < list.size() - 1; j++)
                    list.set(j, list.get(j) + list.get(j + 1));
                
                res.add(new ArrayList<Integer>(list));
            }
            
            return res;
        }
    }

    二刷:

    主要还是使用一个list作为辅助,从1 到 numRows开始遍历,先在level末尾加1,使用memorization倒序遍历level就可以了。

    Java:

    Time Complexity - O(n), Space Complexity - O(n)。     n =  numRows

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            if (numRows < 1) {
                return res;
            }
            List<Integer> level = new ArrayList<>();
            for (int i = 1; i <= numRows; i++) {
                level.add(1);
                for (int j = level.size() - 2; j >= 1; j--) {
                    level.set(j, level.get(j - 1) + level.get(j)); 
                }
                res.add(new ArrayList<Integer>(level));
            }
            return res;
        }
    }

    三刷:

    跟二刷一样,利用一个buffer  curRow来做memorization。 每次先在curRow尾部加1,然后从curRow.size() - 2倒序遍历到1, 设置curRow.set(j, curRow.get(j) + curRow.get(j + 1)),遍历完比以后加到结果集里,最后返回结果集就可以了。

    Java:

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            if (numRows <= 0) {
                return res;
            }
            List<Integer> curRow = new ArrayList<>();
            for (int i = 1; i <= numRows; i++) {
                curRow.add(1);
                for (int j = curRow.size() - 2; j >= 1; j--) {
                    curRow.set(j, curRow.get(j) + curRow.get(j - 1));
                }
                res.add(new ArrayList<Integer>(curRow));
            }
            return res;
        }
    }

    Update:

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            if (numRows <= 0) return res;
            List<Integer> list = new ArrayList<>();
            for (int i = 1; i <= numRows; i++) {
                list.add(1);
                for (int j = list.size() - 2; j > 0; j--) {
                    list.set(j, list.get(j) + list.get(j - 1));
                }
                res.add(new ArrayList<>(list));
            }
            return res;
        }
    }

    Reference:

    https://leetcode.com/discuss/59669/beat-100%25-fastest-java-solution-with-brief-explanation

    https://leetcode.com/discuss/20606/my-concise-solution-in-java

  • 相关阅读:
    Centos7.2升级内核至3.10.0-957【转】
    部署一套完整的Kubernetes高可用集群(上)【转】
    Nginx配置中一个不起眼字符"/"的巨大作用,失之毫厘谬以千里【转】
    使用vmware搭建k8s集群【转】
    nginx的request body日志格式配置
    nginx漏洞修复:SSL/TLS 服务器瞬时 Diffie-Hellman 公共密钥过弱【原理扫描】【转】
    nginx:[warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    linux系统删除分区
    Linux用户锁定、解锁及锁定查看
    innobackupex远程备份【转】
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4438449.html
Copyright © 2011-2022 走看看