forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpascal_tri.py
52 lines (43 loc) · 1023 Bytes
/
pascal_tri.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
from __future__ import division
import random
'''
Leetcode: Pascal's Triangle
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]
]'''
def pascal(n):
if n == 0: return []
rows = [[1]]
row = [1]
for i in range(1,n):
row = [1] + [row[j]+row[j+1] for j in range(len(row)-1)] + [1]
rows.append(row)
print '\n'.join(map(str,rows))
return rows
'''
Leetcode: Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3, return [1,3,3,1].
Note: Could you optimize your algorithm to use only O(k) extra space?
'''
def pascal2(k):
# k-th row: C(k,0), C(k,1), ..., C(k,k)
# C(k,i+1) = C(k,i) * (k-i) / (i+1)
row = [1]
C = 1
for i in range(k):
C *= (k-i)/(i+1)
row.append( int(C) )
print row
return row
if __name__ == '__main__':
pascal(5)
pascal2(4)