119. Pascal’s Triangle II

Information

  • Diffculty: Easy

  • Created: 2020-03-11 07:59:00

  • Last Motified: 2020-03-11 08:06:00

  • Tags: Array

Intuition

The kth row have k + 1 values.

Since the value of nums[i][j] = nums[i - 1][j - 1] + nums[i - 1][j], so we can’t change j - 1 before we use j. So we update the array from right to left.

Solution

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        row = [1] * (rowIndex + 1)
        for i in range(2, rowIndex + 1):
            for j in range(i - 1, 0, -1):
                row[j] += row[j - 1]
        return row