題目:java
Given an index k, return the kth row of the Pascal's triangle.spa
For example, given k = 3,
Return [1,3,3,1]
. code
Note:
Could you optimize your algorithm to use only O(k) extra space? blog
思路:遞歸
遞歸get
package recursion; import java.util.List; import java.util.ArrayList; public class PascalsTriangleII { public List<Integer> getRow(int rowIndex) { List<Integer> res; if (rowIndex == 0) { res = new ArrayList<Integer>(); res.add(1); } else { res = getRow(rowIndex - 1); for (int i = res.size() - 1; i > 0; --i) res.set(i, res.get(i) + res.get(i - 1)); res.add(1); } return res; } }