• 118. 杨辉三角


    package _118;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * https://leetcode.cn/problems/pascals-triangle/
     * 118. 杨辉三角
     * 给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
     * 在「杨辉三角」中,每个数是它左上方和右上方的数的和。
     * 2022-08-29 14:54
     */
    public class Test {
        public static void main(String[] args) {
            int numRows = 5;
            Solution s = new Solution();
            List<List<Integer>>  list = s.generate(numRows);
            System.out.println(list);
        }
    }
    
    class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> list = new LinkedList<>();
    
            List<Integer> tempList= new LinkedList<>();
            tempList.add(1);
            list.add(tempList);
    
    
            //一共有numRows层,从第二层开始
            for(int i = 2;i<=numRows;i++){
                //第i行有i个元素
                // 1
                // 注意,LinkedList的index 是从0开始的
    
                //上一行元素
                List<Integer> preList= tempList;
    
                tempList.clear();
                tempList.add(1);
                for(int j = 0;j< i-2;j++){
                    tempList.add(preList.get(j)+preList.get(j+1));
                }
                tempList.add(1);
    
                list.add(tempList);
            }
    
            return list;
        }
    }
    /**
     * tempList
     * 修改这个 把之前的数据也修改了
     * 
     * ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j) 这样获得上一行的数据
     * 
     * List row = new ArrayList(); 用这个添加新的一行的元素
     */
    class Solution2 {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> ret = new ArrayList<List<Integer>>();
            for (int i = 0; i < numRows; ++i) {
                List<Integer> row = new ArrayList<Integer>();
                for (int j = 0; j <= i; ++j) {
                    if (j == 0 || j == i) {
                        row.add(1);
                    } else {
                        row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
                    }
                }
                ret.add(row);
            }
            return ret;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    day18 java ​​​​​​​集合Collection的List和Set
    SQL 注入笔记
    高压放大器在超声马达中的应用有哪些
    源码分析 – MyBatis Plus 多数据源踩坑
    读书笔记-深度记忆
    基于SSM的客户管理系统设计与实现
    Ansible自动化部署
    【APUE】补充 — 基于管道的线程池
    打造经典游戏:HTML5与CSS3实现俄罗斯方块
    css设置文本属性
  • 原文地址:https://blog.csdn.net/flamesfather/article/details/126586766