LeetCode 솔루션 분류
[7/19] 118. Pascal's Triangle
본문
118. Pascal's Triangle
Easy
6587226Add to ListShareGiven an integer numRows
, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1 Output: [[1]]
Constraints:
1 <= numRows <= 30
Accepted
877,831
Submissions
1,321,677
관련자료
-
링크
댓글 1
학부유학생님의 댓글
- 익명
- 작성일
Runtime: 31 ms, faster than 93.47% of Python3 online submissions for Pascal's Triangle.
Memory Usage: 13.9 MB, less than 66.48% of Python3 online submissions for Pascal's Triangle.
Memory Usage: 13.9 MB, less than 66.48% of Python3 online submissions for Pascal's Triangle.
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
ans = [[1]]
for i in range(1, numRows):
part = []
if len(ans[-1]) == 1:
ans.append([1,1])
else:
part.append(1)
for j in range(len(ans[-1]) - 1):
part.append(ans[-1][j] + ans[-1][j+1])
part.append(1)
ans.append(part)
return ans