Skip to content

Commit fbadd7f

Browse files
committed
Updated pascals triangle 2
1 parent 312b032 commit fbadd7f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Easy/PascalsTriangle2.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
class PascalsTriangle2 {
1616

1717
public static void main(String[] args) {
18+
PascalsTriangle2 p = new PascalsTriangle2();
1819
int k = 3;
19-
System.out.println(getRow(k).toString());
20+
System.out.println(p.getRow(k).toString());
2021
}
2122

2223
/**
2324
* Generate in-place within a list
25+
* 0, 0, 0, 0, initialized, 1, 0, 0, 0
26+
* i = 1, 1, 1, 0, 0
27+
* i = 2, 1, 2, 1, 0
28+
* i = 3, 1, 3, 3, 1
2429
*/
25-
public static List<Integer> getRow(int rowIndex) {
26-
List<Integer> row = new ArrayList<Integer>(rowIndex + 1);
30+
public List<Integer> getRow(int k) {
31+
List<Integer> row = new ArrayList<Integer>(k + 1);
2732
row.add(1);
28-
for (int i = 1; i <= rowIndex; i++) {
29-
for (int j = i - 1; j >= 1; j--) { // backwards
33+
for (int i = 1; i <= k; i++) { // repeat k times
34+
for (int j = i - 1; j >= 1; j--) { // do it backwards
3035
row.set(j, row.get(j - 1) + row.get(j));
3136
}
3237
row.add(1); // add 1 at the end

0 commit comments

Comments
 (0)