-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
package info.mitcc.leetcode; | ||
/* | ||
* Given an index k, return the kth row of the Pascal's triangle. | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
* For example, given k = 3, | ||
* Return [1,3,3,1]. | ||
* Note: | ||
* Could you optimize your algorithm to use only O(k) extra space? | ||
*/ | ||
public class PascalTriangleII { | ||
public ArrayList<Integer> getRow(int rowIndex) { | ||
if(rowIndex < 0) | ||
throw new IllegalArgumentException(); | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
list.add(1); | ||
if(rowIndex == 0) | ||
return list; | ||
else { | ||
ArrayList<Integer> tempList = this.getRow(rowIndex - 1); | ||
for(int i = 1; i < rowIndex; i++) | ||
list.add(tempList.get(i - 1) + tempList.get(i)); | ||
list.add(1); | ||
return list; | ||
} | ||
/**************************** updated 2013/11/26 *********************/ | ||
public ArrayList<Integer> getRow(int rowIndex) { | ||
ArrayList<Integer> res = new ArrayList<Integer>(), temp = new ArrayList<Integer>(); | ||
res.add(1); | ||
temp.add(1); | ||
temp.add(1); | ||
for(int i = 1; i <= rowIndex; i++) { | ||
res = new ArrayList<Integer>(); | ||
res.add(1); | ||
for(int j = 0; j <= i - 2; j++) | ||
res.add(temp.get(j) + temp.get(j + 1)); | ||
res.add(1); | ||
temp = res; | ||
} | ||
return res; | ||
} | ||
|
||
/*********************************************************************/ | ||
|
||
public ArrayList<Integer> getRow(int rowIndex) { | ||
ArrayList<Integer> res = new ArrayList<Integer>(); | ||
res.add(1); | ||
if(rowIndex > 0) { | ||
ArrayList<Integer> temp = getRow(rowIndex - 1); | ||
for(int i = 0; i <= rowIndex - 2; i++) | ||
res.add(temp.get(i) + temp.get(i + 1)); | ||
res.add(1); | ||
} | ||
return res; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
System.out.println(new PascalTriangleII().getRow(in.nextInt())); | ||
} | ||
} |