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) { 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; } }