aboutsummaryrefslogtreecommitdiff
path: root/pascals_triangle.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-01 12:56:20 +0100
committerJack Sangdahl <[email protected]>2025-08-01 12:56:20 +0100
commit0434a2a41dfb2a3b17f785cb07de16c5b09b0d56 (patch)
treedab7a3195fff16c580ea5a56be09c4f2367856e7 /pascals_triangle.py
parent432f582bd1d66c3b87fd365846a620d4a4d9f8a8 (diff)
downloadleetcode-0434a2a41dfb2a3b17f785cb07de16c5b09b0d56.tar.gz
leetcode-0434a2a41dfb2a3b17f785cb07de16c5b09b0d56.zip
118. pascals triangle
Diffstat (limited to 'pascals_triangle.py')
-rw-r--r--pascals_triangle.py13
1 files changed, 6 insertions, 7 deletions
diff --git a/pascals_triangle.py b/pascals_triangle.py
index 0b2bf08..042f823 100644
--- a/pascals_triangle.py
+++ b/pascals_triangle.py
@@ -1,19 +1,18 @@
# 118. Pascal's Triangle
"""
-given an integer 'num_rows', return the first numrows of pascal's triangle.
+given an integer 'numRows', return the first numrows of pascal's triangle.
in pascal's triangle, each number is the sum of the two number direction
above it as show [gif]
"""
class Solution(object):
- def generate(self, num_rows):
- ans = [[1 for _ in range(row + 1)] for row in range(num_rows)]
- for row in range(num_rows):
- for col in range(1, row):
- ans[row][col] = ans[row - 1][col] + ans[row - 1][col - 1]
- return ans
+ def generate(self, numRows):
+ ans = [[1]]
+ for i in range(1, numRows):
+ ans += [map(lambda x, y: x + y, ans[-1] + [0], [0] + ans[-1])]
+ return ans[:numRows]
if __name__ == "__main__":