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

    杨辉三角。按杨辉三角形的计算规则,每一层的第i个位置的元素,等于上一层第i - 1与第i个位置之和。具体代码如下:

    public class Solution {
    
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> arrayList = new ArrayList<List<Integer>>();
            for (int i = 0; i < numRows; i++) {
                List<Integer> arrListFirst = new ArrayList<Integer>();
                for (int j = 0; j <= i; j++) {
                    // 每一行第一位和最后一位为1
                    if (j == 0 || j == i) 
                        arrListFirst.add(1);
                    // 计算中间位置的数
                    else 
                        arrListFirst.add(arrayList.get(i - 1).get(j - 1) + arrayList.get(i - 1).get(j));
                }
                arrayList.add(arrListFirst);
            }
            return arrayList;
        }


  • 相关阅读:
    安装pgsql
    ln软连接
    vsftp上传失败
    redis配置systemctl
    jmeter 录制排除模式
    数据库基本操作
    按日期排序
    angularjs的cache
    angularjs路由传递参数
    angularjs路由相关知识
  • 原文地址:https://www.cnblogs.com/zeroingToOne/p/7800869.html
Copyright © 2011-2022 走看看