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<int> getRow(int rowIndex) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> matrix(rowIndex+1); if(rowIndex < 0) return matrix; for(int i = 0; i <= rowIndex; ++i) { if( i == 0) {matrix[0] = 1;continue;} int mid = i/2; int tmpnum = matrix[0]; for(int j = 1; j <= mid; ++j) { int tmp = tmpnum + matrix[j]; tmpnum = matrix[j]; matrix[j] = tmp; } ++mid; while(mid <= i){ matrix[mid] = matrix[i-mid]; ++mid; } } return matrix; }