Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d81c96a

Browse files
authored
Update pascal_triangle.py
1 parent 407a5d2 commit d81c96a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎pascal_triangle.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,53 @@ def generate(numRows):
4545
res.append(t)
4646
print(res)
4747
return res
48+
49+
--------------------------------------------
50+
class Solution:
51+
def generate(self, numRows: int) -> list[list[int]]:
52+
def generate_row(index: int) -> list[int]:
53+
factorials = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000, 51090942171709440000, 1124000727777607680000, 25852016738884976640000, 620448401733239439360000, 15511210043330985984000000, 403291461126605635584000000, 10888869450418352160768000000, 304888344611713860501504000000, 8841761993739701954543616000000, 265252859812191058636308480000000, 8222838654177922817725562880000000, 263130836933693530167218012160000000, 8683317618811886495518194401280000000]
54+
55+
def comb(n: int, r: int) -> int:
56+
return factorials[n] // (factorials[r] * factorials[n - r])
57+
58+
row = []
59+
for i in range(index + 1):
60+
row.append(comb(index, i))
61+
return row
62+
63+
result = []
64+
for i in range(numRows):
65+
result.append(generate_row(i))
66+
return result
67+
---------------------------------
68+
69+
#my own another function 2025 1 aug
70+
71+
class Solution:
72+
def generate(self, numRows: int) -> List[List[int]]:
73+
74+
if numRows == 1:
75+
return [[1]]
76+
77+
res = [[1],[1,1]]
78+
79+
if numRows ==2:
80+
return res
81+
82+
for i in range(3,numRows+1):
83+
prev = res[-1]
84+
cur = []
85+
86+
cur.append(1)
87+
for j in range(len(prev)-1):
88+
cur.append(prev[j] + prev[j+1])
89+
cur.append(1)
90+
91+
92+
93+
res.append(cur)
94+
95+
print(res)
96+
return res
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /