zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源


    https://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].


    题意分析


    Input:integer

    Output:kth row of the Pascal's triangle

    Conditions:只返回第n行


    题目思路


    同118,不同之处在于只返回某一层,同时注意与上题的下标起点不一样(rowIndex先加个1 = =)


    AC代码(Python)

     1 class Solution(object):
     2     def getRow(self, rowIndex):
     3         """
     4         :type rowIndex: int
     5         :rtype: List[int]
     6         """
     7         rowIndex += 1
     8         ans = []
     9         if rowIndex == 0:
    10             return []
    11         for i in range(rowIndex):
    12             this = []
    13             for j in range(i+1):
    14                 this.append(1)
    15             if i > 1:
    16                 for x in range(i - 1):
    17                     this[x+1] = ans[i-1][x] + ans[i-1][x+1]
    18             print this
    19             ans.append(this)
    20         
    21         return ans[rowIndex - 1]
  • 相关阅读:
    校园路的伤感
    IBM决赛的相片
    IBM一面blue面筋(D组)
    解读校园路
    learn english
    DoNews.COM确实不错
    ARC使用
    Mac 终端 加tab键索引功能
    制作越狱ios设备ipa包
    objc>JS通信及JS>objc通信
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5523954.html
Copyright © 2011-2022 走看看