/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArrayByParity(int* A, int ASize, int* returnSize){
int i=0,j=ASize-1;
int temp=0;
while(i<j){
if(A[i]%2!=0 && A[j]%2==0){
temp=A[j];
A[j]=A[i];
A[i]=temp;
}
if(A[i]%2==0)
i++;
if(A[j]%2!=0)
j--;
}
*returnSize=ASize;
return A;
}
运行效率如下所示:
第10题:转置矩阵 试题要求如下:
回答(C语言):
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** transpose(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
int** num=(int**)malloc(sizeof(int*)*(*AColSize));
*returnColumnSizes=(int*)malloc(sizeof(int)*(*AColSize));
*returnSize=*AColSize;
//分配内存
for(int i=0;i<*AColSize;i++){
num[i]=(int*)malloc(sizeof(int)*ASize);
returnColumnSizes[0][i] = ASize;
}
//矩阵转置
for(int i=0;i<ASize;i++){
for(int j=0;j<*AColSize;j++){
num[j][i]=A[i][j];
}
}
return num;
}