-
Notifications
You must be signed in to change notification settings - Fork 0
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
icafe
committed
Dec 11, 2014
1 parent
41d56f3
commit 7927747
Showing
2 changed files
with
43 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* | ||
* this is solution of Pascal's Triangle | ||
* | ||
* https://oj.leetcode.com/problems/pascals-triangle/ | ||
* | ||
*/ | ||
public class PascalTriangle { | ||
|
||
public List<List<Integer>> generate(int numRows) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
|
||
if (numRows <= 0) { | ||
return result; | ||
} | ||
|
||
for (int m = 1; m <= numRows; m++) { | ||
result.add(generateRow(m)); | ||
} | ||
return result; | ||
} | ||
|
||
public List<Integer> generateRow(int row) { | ||
ArrayList<Integer> last = new ArrayList<>(); | ||
last.add(1); | ||
|
||
for (int i = 1; i < row; i++) { | ||
for (int j = last.size() - 2; j >= 0; j--) { | ||
last.set(j + 1, last.get(j) + last.get(j + 1)); | ||
} | ||
last.add(1); | ||
} | ||
return last; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new PascalTriangle().generate(9)); | ||
} | ||
} |