Skip to content

Commit

Permalink
Solution of Pascal's Triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
icafe committed Dec 11, 2014
1 parent 41d56f3 commit 7927747
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
11 changes: 0 additions & 11 deletions java/Pascal's Triangle/Solution.java

This file was deleted.

43 changes: 43 additions & 0 deletions java/com.gocoder.leetcode/PascalTriangle.java
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));
}
}

0 comments on commit 7927747

Please sign in to comment.