• 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

  • 相关阅读:
    吴恩达机器学习笔记 —— 19 应用举例:照片OCR(光学字符识别)
    吴恩达机器学习笔记 —— 17 推荐系统
    吴恩达机器学习笔记 —— 13 支持向量机
    吴恩达机器学习笔记 —— 15 降维
    SAP MM 无价值物料管理的一种实现思路
    第一节 电商
    e3mall_day09
    activemq整合spring的配置
    log4j简介
    ActiveMQ的入门使用
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4438449.html
Copyright © 2020-2023  润新知