标签:
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) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; i++) {
List<Integer> tmp = new ArrayList<Integer>();
if (i == 0) tmp.add(1);
else {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) tmp.add(1);
else tmp.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
}
}
list.add(tmp);
}
return list;
}
}
标签:
原文地址:http://blog.csdn.net/u011345136/article/details/46006639