Description:

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> tList = new ArrayList<Integer>();
            if(i == 0) {
                tList.add(1);
            }
            else {
                for(int j=0; j<=i; j++) {
                    if(j == 0 || j == i) {
                    tList.add(1);
                }
                else {
                    tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
                }
                }
            }
            list.add(tList);
        }
        return list;
    }
}

 

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2021-06-06
猜你喜欢
  • 2022-12-23
  • 2021-09-01
  • 2021-07-18
  • 2021-08-02
  • 2021-05-22
  • 2021-08-06
  • 2022-02-15
相关资源
相似解决方案