[LeetCode] 118. 杨辉三角

题目链接 : https://leetcode-cn.com/problems/pascals-triangle/

题目描述:

给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

思路:

模拟过程

简单实现过程

def generate(self, numRows: int) -> List[List[int]]:
        res = []
        while numRows:
            tmp = [1]
            if not res:
                res.append(tmp)
            else:
                n = len(res[-1])
                for i in range(n - 1):
                    tmp.append(res[-1][i] + res[-1][i+1])
                tmp.append(1)
                res.append(tmp)
            numRows -= 1
        return res

接下来, 我们把代码简化!

相似题型:119. 杨辉三角 II

代码:

def generate(self, numRows: int) -> List[List[int]]:
        res = []
        tmp = []
        for _ in range(numRows):
            tmp.insert(0, 1)
            for i in range(1, len(tmp) - 1):
                tmp[i] = tmp[i] + tmp[i+1]
            res.append(tmp[:])
        return res

java

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> tmp = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            tmp.add(0, 1);
            for (int j = 1; j < tmp.size() - 1; j++) {
                tmp.set(j, tmp.get(j) + tmp.get(j + 1));
            }
            res.add(new ArrayList<>(tmp));
        }
        return res;
    }
}

猜你喜欢

转载自www.cnblogs.com/powercai/p/11129016.html
今日推荐