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?
Analysis:
滚动数组实现.
C++
vector<int> getRow(int rowIndex) {vector<int> result(rowIndex+2,0);result[1]=1;for(int i=0;i<rowIndex;i++){for(int j=rowIndex+1;j>0;j--){result[j]= result[j-1]+result[j];}}result.erase(result.begin());return result;}
java
public List<Integer> getRow(int rowIndex) {List<Integer> result = new ArrayList<>();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-1)+result.get(j));}result.remove(0);return result;}
Java