119. Pascal’s Triangle II My Submissions QuestionEditorial Solution
Total Accepted: 74481 Total Submissions: 230248 Difficulty: Easy
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?
Subscribe to see which companies asked this question
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getRow(int rowIndex, int* returnSize) {
*returnSize = rowIndex + 1;
int * A = (int *)malloc(sizeof(int)*(rowIndex +1));
A[0]=1;
for(int k=1;k
if(j==k) A[j]=1;
else{
A[j]=A[j]+A[j-1];
}
}
}
return A;
}
一开始使用连乘即C(k,n)的算法,后来发现longlong都装不下,因此只好参考网上思路,
即在O(k)大小内把整个杨辉三角重现。
注意,第一次初始化第0行为1,第一次循环构造1 1,第二次循环构造1 2 1;以此类推。
(太渣。。。)
参考:http://www.cnblogs.com/remlostime/archive/2012/11/13/2767716.html