标签:
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]
]
Subscribe to see which companies asked this question
解题分析:
题目的这个帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。
具体算法如下,我们分两种情况来讨论,
1、第一个元素[1]。
2、从第二个元素开始符合上面数相加结果,最后在末尾补上一个 1
那么我们首先 result.append([1]).然后进行判断,执行公式:
ans = result[i].append(result[i - 1][j] + result[i - 1][j - 1])
# -*- coding: utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def generate(self, numRows):
result = []
for i in range(numRows):
result.append([1])
for j in range(1, i + 1):
if j == 1:
result.append(1)
else:
result[i].append(result[i - 1][j] + result[i - 1][j - 1])
return result
(LeetCode)Pascal's Triangle --- 杨辉三角
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/52149104