Pascal’s Triangle II – LeetCode 119
Problem
Description
Given an index k, return the $k^{th}$ row of the Pascal’s triangle.
Note:
Could you optimize your algorithm to use only O(k) extra space?
Example
For example, given k = 3,
Return [1,3,3,1].
Answer
Original
Code
1 | class Solution { |
思路
同上一题,时间复杂度$O(n^2)$,空间复杂度$O(n)$。
Better
思路
还有就是组合公式法,但恕我直言,考虑到连续的阶乘极有可能导致溢出,所以并不提倡,就不写了。