// face questions 45: the minimum number of array arranged // Title: enter a positive integer array, the array of all the numbers arranged in stitching together a number of prints can fight // all of the digital interface to a minimum. 3 input array {e.g., 32, 321}, then print this number 3 // minimum number of words which can be arranged to 321,323. #include " cstdio " #include < String > #include <algorithm> #pragma warning (disable: 4996) // less ignored safety warnings vs int the Compare ( const void * strNumber1, const void * strNumber2); // int type in decimal integer at most 10 const int g_MaxNumberLength = 10 ; char G_StrCombine1 = * new new char [g_MaxNumberLength * 2 + . 1 ]; char * = g_StrCombine2 new new char [g_MaxNumberLength * 2 + . 1 ]; void PrintMinNumber ( const int * Numbers, int length) { IF (Numbers == nullptr a length || <= 0 ) return ; // array into a string, a two-dimensional array Note ** char ** strNumber = ( char **) ( new new int [length]); for ( int i = 0; i < length; ++i) { strNumber[i] = new char[g_MaxNumberLength + 1]; sprintf(strNumber[i], "%d", numbers[i]); } qsort(strNumber, length, sizeof(char*), compare); for (int i = 0; i < length; ++i) printf("%s", strNumber[i]); printf("\n"); for (int= i 0 ; i <length; ++ i) // remember to create a two-dimensional array to delete the Delete [] strNumber [i]; the Delete [] strNumber; } // if [strNumber1] [strNumber2]> [ strNumber2] [strNumber1 ], the return value is greater than 0 // If [strNumber1] [strNumber2] = [ strNumber2] [strNumber1], the return value is equal to 0 // If [strNumber1] [strNumber2] <[ strNumber2] [strNumber1], returns a value less than 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); }
// ====================测试代码==================== void Test(const char* testName, int* numbers, int length, const char* expectedResult) { if (testName != nullptr) printf("%s begins:\n", testName); if (expectedResult != nullptr) printf("Expected result is: \t%s\n", expectedResult); printf("Actual result is: \t"); PrintMinNumber(numbers, length); printf("\n"); } void Test1() { int numbers[] = { 3, 5, 1, 4, 2 }; Test("Test1", numbers, sizeof(numbers) / sizeof(int), "12345"); } void Test2() { int numbers[] = { 3, 32, 321 }; Test("Test2", numbers, sizeof(numbers) / sizeof(int), "321323"); } void Test3() { int numbers[] = { 3, 323, 32123 }; Test("Test3", numbers, sizeof(numbers) / sizeof(int), "321233233"); } void Test4() { int numbers[] = { 1, 11, 111 }; the Test ( " Test4 " , Numbers, the sizeof (Numbers) / the sizeof ( int ), " 111111 " ); } // array of only a digital void Test5 should be conducted () { int Numbers [] = { 321 }; the Test ( " Test5 should be conducted " , Numbers, the sizeof (Numbers) / the sizeof ( int ), " 321 " ); } void Test6 () { the Test ( "Test6", nullptr, 0, "Don't print anything."); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; }
Analysis: fast row algorithm.
class Solution { public: string PrintMinNumber(vector<int> numbers) { int length = (int) numbers.size(); string strNumber = ""; sort(numbers.begin(), numbers.end(), compare); for (int i = 0; i < length; ++i) strNumber += to_string(numbers[i]); return strNumber; } static bool compare(int strNumber1, int strNumber2) { string g_StrCombine1 = ""; string g_StrCombine2 = ""; //[strNumber1][strNumber2] g_StrCombine1 += to_string(strNumber1); g_StrCombine1 += to_string(strNumber2); //[strNumber2][strNumber1] g_StrCombine2 += to_string(strNumber2); g_StrCombine2 += to_string(strNumber1); return g_StrCombine1 < g_StrCombine2; } };