728x90
class Solution:
# @return a list of lists of integers
def generate(self, numRows):
result = []
for i in range(numRows):
result.append([])
for j in range(i + 1):
if j in (0, i): # i==j(far right) or (j,0) (far left)
result[i].append(1)
else:
result[i].append(result[i - 1][j - 1] + result[i - 1][j]) # left-leftup + left-up
return result
array 채우기
for j in range(i+1): fill row element
Pascal's Triangle II
다른건 i range를 +1 해주고 return시 rowIndex로
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
result = []
for i in range(rowIndex+1):
result.append([])
# print(result)
for j in range(i + 1):
if j in (0, i):
result[i].append(1)
else:
result[i].append(result[i - 1][j - 1] + result[i - 1][j])
return result[rowIndex]
출처: github.com/jiapengwen/LeetCode/blob/master/Python/pascals-triangle.py
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Best Time to Buy and Sell Stock II python (greedy) (0) | 2021.04.01 |
---|---|
[LeetCode] Best Time to Buy and Sell Stocks python (0) | 2021.04.01 |
[LeetCode] Path Sum 파이썬 (0) | 2021.04.01 |
[LeetCode] Minimum Depth of Binary Tree 파이썬 (recursion) (0) | 2021.04.01 |
[LeetCode] Balance Binary Tree python 파이썬 (recursion) (0) | 2021.04.01 |
댓글