Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return[1,3,3,1]
.Note:
Could you optimize your algorithm to use only O(k) extra space?
算法:
思路1:
跟[leetcode]Pascal's Triangle 几乎一样,用2k个空间就可以搞定
思路2:
对空间进行优化,用k个空间搞定
1 public List<Integer> getRow(int rowIndex) { 2 List<Integer> result = new ArrayList<Integer>(); 3 if(rowIndex < 0) return result; 4 result.add(1); 5 for(int i = 1 ; i <= rowIndex;i++){ 6 result = getNextRow(result); 7 } 8 return result; 9 } 10 private List<Integer> getNextRow(List<Integer> list){ 11 int tem = 1; 12 for(int i = 1; i < list.size(); i++){ 13 int a = list.get(i); 14 list.set(i, list.get(i) + tem); 15 tem = a; 16 } 17 list.add(1); 18 return list; 19 }