原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/

题意:

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

解题思路:思路同上一道题。

class Solution:
    # @return a list of integers
    def getRow(self, rowIndex):
        if rowIndex == 0: return [1]
        if rowIndex == 1: return [1, 1]
        list = [[] for i in range(rowIndex+1)]
        list[0] = [1]
        list[1] = [1, 1]
        for i in range(2, rowIndex+1):
            list[i] = [1 for j in range(i + 1)]
            for j in range(1, i):
                list[i][j] = list[i - 1][j - 1] + list[i - 1][j]
        return list[rowIndex]

 

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-10-08
  • 2022-01-12
  • 2022-12-23
  • 2021-04-28
  • 2021-04-17
  • 2022-01-18
猜你喜欢
  • 2022-12-23
  • 2022-01-27
  • 2022-02-23
  • 2022-02-26
  • 2021-08-12
  • 2021-10-07
相关资源
相似解决方案