您的位置:首页 > 其它

118. Pascal's Triangle

2016-06-08 11:38 267 查看
题目:https://leetcode.com/problems/pascals-triangle/

代码:

public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
ArrayList t1 = new ArrayList<>();
ArrayList t2 = new ArrayList<>();
t1.add(1);
t2.add(1);t2.add(1);
if(numRows==0)
return res;
res.add(t1);
if(numRows==1)
{
return res;
}
res.add(t2);
if(numRows==2)
{
return res;
}
for(int i=2;i<numRows;i++)
{
ArrayList<Integer> temp = new ArrayList<>();
temp.add(1);
for(int j=1;j<=i-1;j++)
{
temp.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
}
temp.add(1);
res.add(temp);
}
return res;

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