forked from leetcoders/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pascal'sTriangle.java
39 lines (38 loc) · 989 Bytes
/
Pascal'sTriangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Author: Andy, [email protected]
Date: Apr 16, 2013
Problem: Pascal's Triangle
Difficulty: Easy
Source: http://leetcode.com/onlinejudge#question_118
Notes:
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]
]
Solution: .....
*/
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (numRows < 1) return res;
List<Integer> temp = new ArrayList<Integer>();
temp.add(1);
res.add(temp);
for (int i = 1; i < numRows; ++i) {
List<Integer> t = new ArrayList<Integer>();
t.add(1);
for (int j = 1; j < i; ++j) {
t.add(res.get(i-1).get(j-1) + res.get(i-1).get(j));
}
t.add(1);
res.add(t);
}
return res;
}
}