给定一个非负索引k,返回杨辉三角的第k行[n6]
class Solution:
def getRow(self, rowIndex: int):
result = []
if rowIndex <= 0:
return [1]
for index1 in range(1, rowIndex + 2):
data = []
if index1 == 1:
data.append(1)
elif index1 == 2:
data.append(1)
data.append(1)
else:
for index2 in range(0, index1):
if index2 == 0:
data.append(1)
elif index2 > 0 and index2 < index1 - 1:
data.append(result[index1 - 2][index2 - 1] + result[index1 - 2][index2])
else:
data.append(1)
result.append(data)
row = result[rowIndex]
return row
if __name__ == '__main__':
k = int(input())
print(Solution().getRow(k))