其他分享
首页 > 其他分享> > 【leetcode】距离顺序排列矩阵单元格

【leetcode】距离顺序排列矩阵单元格

作者:互联网

 

int cmp(const void* a, const void* b){
    return (abs((*(int**)a)[0]) + abs((*(int**)a)[1])) - (abs((*(int**)b)[0]) + abs((*(int**)b)[1]));
}

int** allCellsDistOrder(int R, int C, int r0, int c0, int* returnSize, int** returnColumnSizes){
    int i,j,pst=0;
    int** arr = (int**)malloc(R*C*sizeof(int*));
    for (i=0; i<R; i++)
    {
        for (j=0; j<C; j++)
        {
            int* unit = (int*)malloc(2*sizeof(int));
            arr[pst++] = unit;
            unit[0] = i-r0;
            unit[1] = j-c0;
        }
    }
    qsort(arr,pst,sizeof(int*),cmp);
    int* retCol = (int*)malloc(pst*sizeof(int));
    for (i=0; i<pst; i++)
    {
        arr[i][0] += r0;
        arr[i][1] += c0;
        retCol[i] = 2;
    }
    *returnColumnSizes = retCol;
    *returnSize = pst;
    return arr;
}

 

标签:顺序排列,arr,const,allCellsDistOrder,int,void,单元格,abs,leetcode
来源: https://www.cnblogs.com/ganxiang/p/13703776.html