程序员面试题精选100题(41)-把数组排成最小的数(算法)(3)

时间:2019-09-22 编辑:多美文
    }

 

    // Sort all strings according to algorithm in function compare

    qsort(strNumbers, length, sizeof(char*), compare);

 

    for(int i = 0; i < length; ++i)

        printf("%s", strNumbers[i]);

    printf("\n");

 

    for(int i = 0; i < length; ++i)

        delete[] strNumbers[i];

    delete[] strNumbers;

}

 

// Compare two numbers in strNumber1 and strNumber2

// if [strNumber1][strNumber2] > [strNumber2][strNumber1],

// return value > 0

// if [strNumber1][strNumber2] = [strNumber2][strNumber1],

// return value = 0

// if [strNumber1][strNumber2] < [strNumber2][strNumber1],

// return value < 0

int compare(const void* strNumber1, const void* strNumber2)

{

    // [strNumber1][strNumber2]

    strcpy(g_StrCombine1, *(const char**)strNumber1);

    strcat(g_StrCombine1, *(const char**)strNumber2);

 

    // [strNumber2][strNumber1]

    strcpy(g_StrCombine2, *(const char**)strNumber2);

    strcat(g_StrCombine2, *(const char**)strNumber1);

 

    return strcmp(g_StrCombine1, g_StrCombine2);

}

本文已影响
相关文章