1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [] for i in range(numRows): row = [1] if i > 0: prev_row = result[i-1] num_elements = (i + 1) // 2 for j in range(1, num_elements): element = prev_row[j-1] + prev_row[j] row.append(element) if i % 2 == 0: odd_row=row[:num_elements] element = prev_row[num_elements - 1] + prev_row[num_elements] row.append(element) row += odd_row[::-1] if i % 2 == 0 else row[::-1] result.append(row) return result
|