[LeetCode] Pascal & #39; 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]]
Iterative non-recursive version
Public class Solution {public List
> Generate (int numRows) {List
> Res = new ArrayList <> (); if (numRows <= 0) return res; List
Lin = new ArrayList <> (); lin. add (1); res. add (lin); if (numRows = 1) return res; for (int I = 0; I
List = new ArrayList <> (); List
Pre = res. get (I); list. add (1); for (int j = 0; j
Recursive versionpublic class Solution {List
> res = new ArrayList<>(); public List
> generate(int numRows) { if(numRows<=0) return res; helpGenerate(numRows); return res; } private List
helpGenerate(int numRows){ if(numRows == 1){ List
list = new ArrayList<>(); list.add(1); res.add(list); return list; } List
pre = helpGenerate(numRows-1); List
list = new ArrayList<>(); list.add(1); for(int i=0;i