Skip to content

Commit ac27b27

Browse files
author
applewjg
committed
Pascal'sTriangle
Change-Id: I205969737b5316955a2c4c79420e9588f4b1650d
1 parent 038ecf9 commit ac27b27

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Pascal'sTriangle.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)