Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5, Return
, , , ,
]
生成杨辉三角。不清楚什么是杨辉三角的构成请参考维基百科-杨辉三角。代码如下:
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
if(numRows == 0) return result;
list.add(1);
result.add(list);
for(int i = 1; i < numRows; i++) {
list = new ArrayList<Integer>();
list.add(1);
for(int j = 0; j < result.get(i - 1).size() - 1; j++) {
list.add(result.get(i - 1).get(j) + result.get(i - 1).get(j + 1));
}
list.add(1);
result.add(list);
}
return result;
}
}