题目:
Given an index k, return the k th 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?
思路:从后面往前面扫描,注意覆盖数据
代码:
import java.util.*; public class Solution { public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> list = new ArrayList<Integer>(); if(rowIndex < 0){ return list; } for(int i = 0; i <= rowIndex; i++){ list.add(0, 1); for(int j = 1; j < i ; j++){ list.set(j, list.get(j + 1) + list.get(j)); } list.set(i, 1); } return list; } }
Given an index k, return the k th 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?