题目来源:
https://leetcode.com/problems/pascals-triangle/
题意分析:
给定一个整数,输出N层的Triangle 。
题目分析:
直接根据Triangle的定理,ans[i][j] = ans[i - 1][j] + ans[i - 1][j + 1]。
代码(python):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 class Solution(object): 2 def generate(self, numRows): 3 """ 4 :type numRows: int 5 :rtype: List[List[int]] 6 """ 7 if numRows == 0: 8 return [] 9 if numRows == 1:10 return [[1]]11 if numRows == 2:12 return [[1],[1,1]]13 i,ans = 3,[[1],[1,1]]14 while i <= numRows:15 tmp = [1]16 for j in range(len(ans[i-2])-1):17 tmp.append(ans[i-2][j] + ans[i-2][j + 1])18 tmp.append(1)19 ans.append(tmp)20 i += 121 return ans