-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalTriangle.java
More file actions
45 lines (43 loc) · 1.53 KB
/
pascalTriangle.java
File metadata and controls
45 lines (43 loc) · 1.53 KB
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
public class Solution {
public ArrayList<ArrayList<Integer>> generate(int numRows) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
if(numRows==0) return result;
ArrayList<Integer> single=new ArrayList<Integer>();
single.add(1);
result.add(single);
for(int row=1;row<numRows;row++) {
single=new ArrayList<Integer>();
single.add(1);
for(int column=1;column<row;column++) {
single.add(result.get(row-1).get(column-1)+result.get(row-1).get(column));
}
single.add(1);
result.add(single);
}
return result;
}
}
public class Solution {
public ArrayList<Integer> getRow(int rowIndex) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> result=new ArrayList<Integer>();
for (int i = 0; i < rowIndex+1; i++) {
result.add(1);
}
for(int row=1;row<=rowIndex;row++) {
int previous=result.get(0);
for(int column=1;column<row;column++) {
int current=result.get(column);
result.set(column,previous+current);
previous=current;
}
for(int column=row-1;column>=1;column--) {
result.set(column,result.get(column-1)+result.get(column));
}
}
return result;
}
}