大家用过countertop soap dispenser吗? (转载)# Living
a*e
1 楼
问道简单题,
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?
对下面这种优化解总觉得不是很清楚。感觉如果不是自己想出来的东西下次又会忘,有
没有同学解释下原理?为啥内循环要从右忘左扫?多谢!
vector getRow(int rowIndex) {
vector res;
if (rowIndex<0) return res;
res.resize(rowIndex+1);
res[0]=1;
for (int i=1; i {
for (int j=i; j>=1; j--)
res[j]=res[j-1]+res[j];
}
return res;
}
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?
对下面这种优化解总觉得不是很清楚。感觉如果不是自己想出来的东西下次又会忘,有
没有同学解释下原理?为啥内循环要从右忘左扫?多谢!
vector
vector
if (rowIndex<0) return res;
res.resize(rowIndex+1);
res[0]=1;
for (int i=1; i
for (int j=i; j>=1; j--)
res[j]=res[j-1]+res[j];
}
return res;
}