您的位置:首页 > 编程语言 > Go语言

[勇者闯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: Array

Difficulty: 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm leetcode