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

Commit611bb7b

Browse files
author
piggy1991
committed
genera
1 parent5bf7ef6 commit611bb7b

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

‎dp/Generate.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎dp/GetRow.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
importjava.util.List;
55

66
publicclassGetRow {
7-
publicList<Integer>getRow(introwIndex) {
7+
publicList<Integer>getRow1(introwIndex) {
88
List<Integer>pre =newArrayList<Integer>();
99
List<Integer>cur =newArrayList<Integer>();
1010

1111
if (rowIndex <0) {
1212
returncur;
1313
}
1414

15-
//注意这里的rowIndex跟上一题的意义不一样!这个是索引,orz...
16-
//所以我们要用<=
15+
//娉ㄦ剰杩欓噷鐨剅owIndex璺熶笂涓�鐨勬剰涔変笉涓�牱锛佽繖涓槸绱㈠紩锛宱rz...
16+
//鎵�互鎴戜滑瑕佺敤<=
1717
for (inti =0;i <=rowIndex;i++) {
18-
//第i行有i +1个元素
18+
//绗琲琛屾湁i +1涓厓绱�
1919
cur =newArrayList<Integer>();
2020
for (intj =0;j <i +1;j++) {
2121
if (j ==0 ||j ==i) {
@@ -29,4 +29,23 @@ public List<Integer> getRow(int rowIndex) {
2929

3030
returncur;
3131
}
32+
33+
// SOLUTION 2: DO IT just inplace
34+
publicList<Integer>getRow(introwIndex) {
35+
List<Integer>ret =newArrayList<Integer>();
36+
37+
for (inti =0;i <=rowIndex;i++) {
38+
for (intj =i;j >=0;j--) {
39+
if (j ==i) {
40+
ret.add(1);
41+
}elseif (j !=0) {
42+
// ERROR: use add instead of set
43+
//ret.add(ret.get(j) + ret.get(j - 1));
44+
ret.set(j,ret.get(j) +ret.get(j -1));
45+
}
46+
}
47+
}
48+
49+
returnret;
50+
}
3251
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp