[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]

 

posted @ 2016-05-24 16:44  loadofleaf  Views(510)  Comments(0Edit  收藏  举报