您的位置:首页 > 其它

Pascal's Triangle

2015-07-03 11:19 330 查看
题目

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]
]

code


public class Solution {
public ArrayList<ArrayList<Integer>> generate(int numRows)
{

ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numRows<=0)
{
return result;
}
ArrayList<Integer> pre = new ArrayList<Integer>();
pre.add(1);
result.add(pre);

for(int i=2; i<=numRows; i++)
{
ArrayList<Integer> cur = new ArrayList<Integer>();
cur.add(1); //first
for (int j=0; j<pre.size()-1;j++)
{
cur.add(pre.get(j)+pre.get(j+1)); //middle
}
cur.add(1); //last

result.add(cur);
pre=cur;

}

return result;

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