File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Author: Andy, nkuwjg@gmail.com
3
+ Date: Apr 16, 2013
4
+ Problem: Pascal's Triangle
5
+ Difficulty: Easy
6
+ Source: http://leetcode.com/onlinejudge#question_118
7
+ Notes:
8
+ Given numRows, generate the first numRows of Pascal's triangle.
9
+ For example, given numRows = 5,
10
+ Return
11
+ [
12
+ [1],
13
+ [1,1],
14
+ [1,2,1],
15
+ [1,3,3,1],
16
+ [1,4,6,4,1]
17
+ ]
18
+
19
+ Solution: .....
20
+ */
21
+ public class Solution {
22
+ public List <List <Integer >> generate (int numRows ) {
23
+ List <List <Integer >> res = new ArrayList <List <Integer >>();
24
+ if (numRows < 1 ) return res ;
25
+ List <Integer > temp = new ArrayList <Integer >();
26
+ temp .add (1 );
27
+ res .add (temp );
28
+ for (int i = 1 ; i < numRows ; ++i ) {
29
+ List <Integer > t = new ArrayList <Integer >();
30
+ t .add (1 );
31
+ for (int j = 1 ; j < i ; ++j ) {
32
+ t .add (res .get (i -1 ).get (j -1 ) + res .get (i -1 ).get (j ));
33
+ }
34
+ t .add (1 );
35
+ res .add (t );
36
+ }
37
+ return res ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments