Leetcode ---119. 杨辉三角 II(数组)

119. 杨辉三角 II

  • 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
  • 在杨辉三角中,每个数是它左上方和右上方的数的和。

示例1:

输入: 3
输出: [1,3,3,1]

python

思路:

  1. 与杨辉三角的思路一致。
class Solution:    
   def getRow(self, rowIndex: int) -> List[int]:        
       yh=[[]]*(rowIndex+1)        
       for row in range(len(yh)):            
           yh[row]=[None]*(row+1)            
           for col in range(len(yh[row])):                
              if col==0 or col==row:                    
                 yh[row][col]=1                
              else:                    
                 yh[row][col]=yh[row-1][col-1]+yh[row-1][col]        
       return yh[-1]

你可能感兴趣的:(Leetcode,数组)