[勇者闯LeetCode] 118. Pascal's Triangle
2017-04-29 21:11
393 查看
[勇者闯LeetCode] 118. Pascal’s Triangle
Description
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
Information
Tags: ArrayDifficulty: Easy
Solution
找规律:1. new[0]=old[0],
2. new[i]=old[i]+old[i−1],when 0<i<len(old),
3. new[len(old)]=1。
Python Code
class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ if numRows <= 0: return [] result = [[1]] for _ in range(numRows-1): temp = result[-1][:] for i in range(len(temp)-1, 0, -1): temp[i] += temp[i-1] temp += [1] result.append(temp) return result
相关文章推荐
- 【LeetCode】118 Pascal's Triangle
- [leetcode: Python] 118.Pascal's Triangle
- LeetCode笔记:118. Pascal's Triangle
- 【LeetCode从零单排】No118 Pascal's Triangle
- Leetcode_c++: Pascal's Triangle(118)
- leetcode 118 Pascal's Triangle
- leetcode118 Pascal's Triangle
- [LeetCode]--118. Pascal's Triangle
- LeetCode 118 Pascal's Triangle
- LeetCode 118, 119. Pascal's Triangle i, ii
- LeetCode: Pascal's Triangle [118]
- 118. Pascal's Triangle Leetcode Python
- 【LeetCode-面试算法经典-Java实现】【118-Pascal's Triangle(帕斯卡三角形)】
- LeetCode118 Pascal's Triangle
- LeetCode(118) Pascal's Triangle (Java)
- [Leetcode]#118 Pascal's Triangle
- leetcode_118 Pascal Triangle
- leetcode-118-Pascal's Triangle 基础题
- [LeetCode] 118. Pascal's Triangle
- LeetCode-118:Pascal's Triangle (生成指定行数的杨辉三角)