[LeetCode]题解(python):119-Pascal's Triangle II
题目来源:
https://leetcode.com/problems/pascals-triangle-ii/
题意分析:
给定一个整数k,返回第k层的Triangle。
题目思路:
根据Triangle规则,直接计算即可。
代码(python):
class Solution(object): def getRow(self, rowIndex): """ :type rowIndex: int :rtype: List[int] """ if rowIndex == 0: return [1] if rowIndex == 1: return [1,1] ans = [1,1] i = 1 while i < rowIndex: tmp = [1] for j in range(len(ans) - 1): tmp.append(ans[j] + ans[j + 1]) tmp.append(1) ans = tmp[:] i += 1 return ans