Pascal's Triangle (java)

来源:互联网 发布:淘宝新店一天刷几单 编辑:程序博客网 时间:2024/06/12 22:19

Pascal's Triangl

e

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1,4,6,4,1]]


我的解决方案

public class Solution {
public List<List<Integer>> generate(int numRows) {
if(numRows>=1)
{
List<Integer> rows = new ArrayList<Integer>();
List<List<Integer>> numRow = new ArrayList<List<Integer>>();
for(int i = 1; i<=numRows;i++)
{
rows = new ArrayList<Integer>();
int k = 1;
for(int j = 1;j <= i;j++)
{
rows.add(k);
k = k*(i-j)/j;
}

numRow.add(rows);
}

return numRow;
}
else
{
List<List<Integer>> numRow = new ArrayList<List<Integer>>();
return numRow;
}
}
}
0 0