////////////////////////////////////////////////// ////////////////////////////
// 6. Title IV: replace spaces
// Make a function implemented, each of the string replacing spaces with "20%"; for example, enter: "! We Are Happy" -> "! We% 20Are% 20Happy"
// time complexity of O (n), the spatial complexity is O (1)
char * StrReplace ( char * pszStr, int iLen, char CH, const char * pszReStr) { Assert (pszStr = NULL && pszReStr = NULL && iLen> =!! 0 ); char * PTMP = pszStr; int iFindNum = 0 ; // 1. Find the number of string ch the while (* = PTMP! ' \ 0 ' ) { IF (PTMP ++ == * ch) { iFindNum ++ ; } } //2. The new string length int iOldLen = strlen (pszStr); int iReLen = strlen (pszReStr); int iNewLen = iOldLen + (iReLen - 1 ) * iFindNum; // this string length is decremented by 1 to be replaced - the original character length if (iNewLen> iLen) { return NULL; } // 3. assignment from the back string // why there are not i = iOldLen - 1; -> copy the last '\ 0'; for ( int I = iOldLen; I> = 0 && iNewLen> I; i-- ) { IF (pszStr [I] == CH) { for ( int j = iReLen - 1; j >= 0; j--) { pszStr[iNewLen--] = pszReStr[j]; } } else { pszStr[iNewLen--] = pszStr[i]; } } #if 0 while (iOldLen >= 0 && iNewLen > iOldLen) { if (pszStr[iOldLen] == ch) { for (int i = iReLen - 1; i >= 0; i--) { pszStr[iNewLen--] = pszReStr[i]; } } else { pszStr[iNewLen--] = pszStr[iOldLen]; } iOldLen--; } #endif return pszStr; } void StrReplaceTestFunc() { cout << "\n\n --------------- StrReplaceTestFunc Start -------------->" << endl; const int MAX_STR_LEN = 128; char szStr[MAX_STR_LEN] = "We Are Happy !"; StrReplace(szStr, MAX_STR_LEN, ' ', "%20"); cout << "Str: " << szStr << endl; StrReplace(szStr, MAX_STR_LEN, '!', "------->"); cout << "Str: " << szStr << endl; cout << "\n\n --------------- StrReplaceTestFunc End -------------->" << endl; }
////////////////////////////////////////////////// /////////////////
// 2 with four topics related topics
// two sorted array A1, A2, A1 end there is enough space to accommodate A2, achieve a function, the A2, A1 insert all the numbers and all numbers in the orderly!
@ Method a: from front to back comparison, additional space is required // time complexity O (2n), the spatial complexity of O (n-) void MergeTwoArray1 ( int aiArrayA [], int iNumA, int aiArrayB [], int iNumB) { const int MAX_ARRAY_COUNT + = iNumA iNumB; Vector < int > vect (MAX_ARRAY_COUNT, 0 ); int I = 0 , J = 0 , K = 0 ; // 1. comparison of the two arrays, the smaller the added new array the while (I <iNumA && J < iNumB) { IF (aiArrayA [I] < aiArrayB [J]) { vect [K ++] = aiArrayA [I ++ ]; } the else { vect [K ++] = aiArrayB [J ++ ]; } } // 2. The remaining elements added to a new array of the while (I < iNumA) { vect [ K ++] = aiArrayA [I ++ ]; } the while (J < iNumB) { vect [K ++] = aiArrayB [J ++ ]; } // copy the data to the array 3. A K = 0 ; for (Auto IT: vect) { aiArrayA [K ++] =IT; } } // Method Two: Comparative from the back, no extra space // time complexity of O (n), the spatial complexity is O (. 1) void MergeTwoArray2 ( int aiArrayA [], int iNumA, int aiArrayB [], int iNumB) { int iNewNum = iNumA iNumB + - . 1 ; int I = iNumA - . 1 ; int J = iNumB - . 1 ; // forward from the comparison array, the overlap does not exist! ! ! the while (I> = 0 && J> = 0 ) { IF (aiArrayA [I]> aiArrayB [J]) { AiArrayA [iNewNum -] = aiArrayA [i-- ]; } Else { aiArrayA [iNewNum -] = aiArrayB [j-- ]; } } While (i> = 0 ) { aiArrayA [iNewNum -] = aiArrayA [i-- ]; } While (j> = 0 ) { aiArrayA [iNewNum -] = aiArrayB [j-- ]; } } Void MergeTwoArrayTestFunc () { cout << "\n\n --------------- MergeTwoArrayTestFunc Start -------------->" << endl; const int MAX_ARRAY_COUNT_A = 30; const int MAX_ARRAY_COUNT_B = 10; int aiArrayA[MAX_ARRAY_COUNT_A] = {0}; int aiArrayB[MAX_ARRAY_COUNT_B] = {0}; INITIALIZE_ARRAY(aiArrayA, MAX_ARRAY_COUNT_B); INITIALIZE_ARRAY(aiArrayB, MAX_ARRAY_COUNT_B); std::sort(aiArrayA, aiArrayA + MAX_ARRAY_COUNT_B); std::sort(aiArrayB, aiArrayB + MAX_ARRAY_COUNT_B); TRAVERSAL_ARRAY(aiArrayA, MAX_ARRAY_COUNT_B); TRAVERSAL_ARRAY(aiArrayB, MAX_ARRAY_COUNT_B); //MergeTwoArray1(aiArrayA, MAX_ARRAY_COUNT_B, aiArrayB, MAX_ARRAY_COUNT_B); MergeTwoArray2(aiArrayA, MAX_ARRAY_COUNT_B, aiArrayB, MAX_ARRAY_COUNT_B); TRAVERSAL_ARRAY(aiArrayA, MAX_ARRAY_COUNT_A); cout << "\n\n --------------- MergeTwoArrayTestFunc Start -------------->" << endl; }