Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
39 lines (38 loc) · 989 Bytes

File metadata and controls

39 lines (38 loc) · 989 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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, nkuwjg@gmail.com
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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.