您的位置:首页 > 其它

【LeetCode】118.Pascal's Triangle(easy)解题报告

2018-02-11 23:52 525 查看
【LeetCode】118.Pascal’s Triangle(easy)解题报告

题目地址:https://leetcode.com/problems/pascals-triangle/description/

题目描述:

  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]

  ]

Solution:

//time:O(n^2)
//space:O(n)
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
for(int i=0 ; i<numRows ; i++){
list.add(0,1);
for(int j=1 ; j<list.size()-1 ; j++){
list.set(j,list.get(j)+list.get(j+1));
}
res.add(new ArrayList<>(list));
}
return res;
}
}


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