118. Pascal's Triangle
https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
0
Output:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
[]Solution
class Solution(object):
def generate(self, numRows):
if numRows == 0:
return []
Ans = []
Ans.append([1])
for i in range(1,numRows):
Ans.append([])
a = 1
Ans[i].append(1)
while a < i:
Ans[i].append( Ans[i-1][a-1]+Ans[i-1][a] )
a += 1
Ans[i].append(1)
return Ansclass SolutionII(object):
def generate(self, numRows):
Ans = []
for i in range(numRows):
if i <= 1:
pre = [1]*(i+1)
Ans.append( pre )
else:
temp = [1]
for y in range( 1,i ):
temp.append( pre[y-1]+pre[y] )
pre = temp + [1]
Ans.append(pre)
return AnsLast updated