您的位置:首页 > 编程语言 > Go语言

[LeetCode] Pascal's Triangle

2014-04-08 15:59 246 查看
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 ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

for (int i = 0; i < numRows; i++) {
ArrayList<Integer> rowList = new ArrayList<Integer>();
// add an element
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) rowList.add(1);
else rowList.add(list.get(i-1).get(j-1) + list.get(i-1).get(j));
}

// add a row
list.add(rowList);
}

return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Algorithms LeetCode Array