其他分享
首页 > 其他分享> > LeetCode 0118 Pascal's Triangle

LeetCode 0118 Pascal's Triangle

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
r[i][j] = r[i - 1][j - 1] + r[i - 1][j];

2、代码实现

package Q0199.Q0118PascalsTriangle;

import java.util.ArrayList;
import java.util.List;

// r[i][j] = r[i - 1][j - 1] + r[i - 1][j];
public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>();
        ArrayList<Integer> row = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            row.add(0, 1);
            for (int j = 1; j < row.size() - 1; j++)
                row.set(j, row.get(j) + row.get(j + 1));
            result.add(new ArrayList<>(row));
        }
        return result;
    }
}

3、复杂度分析
时间复杂度: O(n^2)
空间复杂度: O(1)

标签:Triangle,int,0118,List,result,ArrayList,new,LeetCode,row
来源: https://www.cnblogs.com/junstat/p/16279438.html