-
Notifications
You must be signed in to change notification settings - Fork 0
/
PascalsTriangle2.java
65 lines (52 loc) · 2.19 KB
/
PascalsTriangle2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
*key point: much like the Pascal's Triangle quesion, the only difference
* is we don't need to store previous row; just return the last row is enough,
* it's even easier than that question.
*
* However, since I didn't realize that my solution for Pascal's Triangle question
* already meets the requirment of O(k) extra space. I tried several other methods
* to calculate the kth row directly with the help of combination formula,
* but I was blocked by data type range overflow
*
* The lesson is always making sure the requirements of questions before coding anything
*/
public class Solution {
public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> result = new ArrayList<Integer>();
if(rowIndex < 0)
return result;
//two ArrayList for current row and last row
ArrayList<Integer> lastRow = new ArrayList<Integer>();
ArrayList<Integer> currentRow = new ArrayList<Integer>();
ArrayList<Integer> temp = null;
//treat first line separately
lastRow.add(1);
if(rowIndex == 0)
return lastRow;
//init lastRow as second line
lastRow.add(1);
if(rowIndex == 1){
return lastRow;
}
//start from third line, lineNum is 0-based index
int lineNum = 2;
for(lineNum = 2;lineNum <= rowIndex;lineNum++){
//add the first '1' to a new row
currentRow.add(1);
//calculate the middle numbers by adding two numbers on its "shoulder"
//within every ArrayList, it's zero-based index
//last row has only lineNum-1 elements
for(int i = 0;i < lineNum-1;i++){
currentRow.add(lastRow.get(i) + lastRow.get(i+1));
}
//add the last '1' to this row
currentRow.add(1);
lastRow.clear();
//the current working row becomes last row, new current row should be empty
temp = lastRow;
lastRow = currentRow;
currentRow = temp;
}
return lastRow;
}
}