您的位置:首页 > 其它

118. Pascal's Triangle (杨辉三角)

2016-10-27 22:52 281 查看
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 LinkedList<List<Integer>>();
List<Integer> intList = new LinkedList<Integer>();
for(int i=0;i<numRows;i++){
for(int j=0;j<=i;j++){
if(j==0||j==i)
intList.add(1);
else
intList.add(list.get(i-1).get(j-1)+list.get(i-1).get(j));
}
list.add(intList);
intList = new LinkedList<Integer>();
}return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法