LeetCode Pascal & #39; s Triangle II
LeetCode-solving-Pascal's Triangle II
Original question
Use the space of O (k) to obtain the value of the k row in the Yang Hui triangle.
Note:
Calculates the number of rows starting from 0, that is, 0th actions [1]
Example:
Input: k = 3
Output: [1, 3, 3]
[[1], [], [, 1], [,], [, 1]
Solutions
Now we need to consider saving space on the basis of Pascal's Triangle. We can know that row k requires (k + 1) space, and the length of the next row is longer than that of the previous row, therefore, it is appropriate to calculate the value of the next row from the back to the back.
AC Source Code
Class Solution (object): def getRow (self, rowIndex): "": type rowIndex: int: rtype: list [int] "" result = [1] * (rowIndex + 1) for I in range (2, rowIndex + 1): for j in range (1, I ): result [I-j] + = result [I-j-1] return resultif _ name _ = "_ main _": assert Solution (). getRow (3) = [1, 3, 3, 1]