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?
动规不解释。
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int *a = new int[rowIndex+1]; 7 vector<int> v; 8 for(int i = 1;i <= rowIndex+1;i++) 9 { 10 for(int j = i - 1;j >= 0;j--) 11 { 12 if(j == 0) 13 a[j] = 1; 14 else if(j == i-1) 15 a[j] = 1; 16 else 17 a[j] = a[j]+a[j-1]; 18 } 19 } 20 for(int j = 0;j <= rowIndex;j++) 21 v.push_back(a[j]); 22 return v; 23 } 24 };

浙公网安备 33010602011771号