public class Solution {
// From end to begin, A[j] = A[j] + A[j-1] =>
// [0,1,0,0,0,0]
// [0,1,1,0,0,0]
// [0,1,2,1,0,0]
// [0,1,3,3,1,0]
// [0,1,4,6,4,1]
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>(rowIndex+2);
for (int i=0; i<rowIndex+2; i++){
result.add(0);
}
result.set(1, 1);
for (int i=0; i<rowIndex; i++){
for (int j=rowIndex+1; j>0; j--){
result.set(j, result.get(j)+result.get(j-1));
}
}
result.remove(0);
return result;
}
}
Pascal's Triangle II
最新推荐文章于 2022-02-07 21:31:57 发布