LeetCode Pascal's Triangle

来源:互联网 发布:关系数据语言的特点 编辑:程序博客网 时间:2024/06/03 07:18

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]]
题意:按照图生成矩阵。

思路:类似杨辉三角的做法。

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



0 0
原创粉丝点击