Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitac27b27

Browse files
author
applewjg
committed
Pascal'sTriangle
Change-Id: I205969737b5316955a2c4c79420e9588f4b1650d
1 parent038ecf9 commitac27b27

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+
publicclassSolution {
22+
publicList<List<Integer>>generate(intnumRows) {
23+
List<List<Integer>>res =newArrayList<List<Integer>>();
24+
if (numRows <1)returnres;
25+
List<Integer>temp =newArrayList<Integer>();
26+
temp.add(1);
27+
res.add(temp);
28+
for (inti =1;i <numRows; ++i) {
29+
List<Integer>t =newArrayList<Integer>();
30+
t.add(1);
31+
for (intj =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+
returnres;
38+
}
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp