# leetcode-最小绝对差

## 思路

/**
* 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 comp(const void* p1,const void* p2){
return *(int*) p1 - *(int*) p2;
}

int Min(int a,int b){
return a<b?a:b;
}

int** minimumAbsDifference(int* arr, int arrSize, int* returnSize, int** returnColumnSizes){
qsort(arr,arrSize,sizeof(int),comp);
int min=INT_MAX;
int count=0;
*returnColumnSizes = (int*)calloc(arrSize - 1, sizeof(int));
for(int i=1;i<arrSize;i++){
min=Min(min,arr[i]-arr[i-1]);
}
for(int i=1;i<arrSize;i++){
if((arr[i]-arr[i-1])==min){
count++;
}
}

int **new_arr=(int**) malloc(sizeof(int*) * count);
*returnSize=count;

for(int i=0;i<count;i++){
(*returnColumnSizes)[i]=2;
new_arr[i]=(int*) malloc(sizeof(int) * 2);
}
int k=0;
for(int i=1;i<arrSize;i++){
if((arr[i]-arr[i-1])==min){
new_arr[k][0]=arr[i-1];
new_arr[k][1]=arr[i];
k++;
}
}
return new_arr;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客