1. 程式人生 > >查找數組中重復的數字

查找數組中重復的數字

都在 log 最大的 自定義 dup clu spa 個數 true

題目來源於《劍指Offer》中的面試題3:找出數組中重復的數字。
  // 題目:在一個長度為n的數組裏的所有數字都在0到n-1的範圍內。數組中某些數字是重復的,但不知道有幾個數字重復了,
  // 也不知道每個數字重復了幾次。請找出數組中任意一個重復的數字。例如,如果輸入長度為7的數組{2, 3, 1, 0, 2, 5, 3},
  // 那麽對應的輸出是重復的數字2或者3。

解決方法有多種,包括數組排序,哈希表法,以及作者推薦的重排數組法。此處介紹自己的一個做法,以空間換時間,通過新建數組來實現快速查找,具體做法是新建長度為length的數組newArray,初始化值為-1;將numbers數組的值依次作為newArray的下標和對應的值為newArray賦值,其中number向newArray賦值時,判斷newArray對應下標是否為-1,如果不為-1則表示newArray曾被相同的數賦值,說明有重復的數存在。以數組{2,3,1,0,2,5,3}為例,新建長度為7的數組{-1,-1,-1,-1,-1,-1,-1},依次賦值為{-1,-1,2,-1,-1,-1,-1},{-1,-1,2,3,-1,-1,-1},{-1,1,2,3,-1,-1,-1},{0,1,2,3,-1,-1,-1},下一次將2賦值給新數組時,發現新數組中下標為2的為2,說明曾被2賦值,說明2是重復的。

#include<cstdio>

// 參數:
//        numbers:     一個整數數組
//        length:      數組的長度
//        duplication: (輸出) 數組中的一個重復的數字
// 返回值:             
//        true  - 輸入有效,並且數組中存在重復的數字
//        false - 輸入無效,或者數組中沒有重復的數字
//bool duplicate(int numbers[], int length, int* duplication)
//{
//    // int* duplication 使用指針類型的形參訪問函數外部的對象,通過指針可以訪問和修改指向的對象,但是拷貝的指針是兩個不同的指針
// // 建議使用引用類型的形參替代指針 // if (numbers == nullptr || length <= 0) // return false; // // for (int i = 0; i < length; ++i) // { // if (numbers[i] < 0 || numbers[i] > length - 1) // return false; // } // // for (int i = 0; i < length; ++i) // { // while (numbers[i] != i)
// { // if (numbers[i] == numbers[numbers[i]]) // { // *duplication = numbers[i]; //解引用 // return true; // } // // // 交換numbers[i]和numbers[numbers[i]] // int temp = numbers[i]; // numbers[i] = numbers[temp]; // numbers[temp] = temp; // } // } // return false; //} /* 我的新方法: 新建長度為length的數組newArray,初始化值為-1; 將numbers數組的值作為newArray的下標和對應的值為newArray賦值, 其中number向newArray賦值時,判斷newArray對應下標是否為-1, 如果不為-1則表示newArray曾被相同的數賦值,說明有重復的數存在 */ bool duplicate(int numbers[], int length, int* duplication) { // 過濾掉空指針和不符合規定的輸入 if (numbers == nullptr || length <= 0) return false; for (int i = 0; i < length; ++i) { if (numbers[i] < 0 || numbers[i] > length - 1) return false; } int* tempArray = new int[length]; //動態數組 for (int * q= tempArray;q!= tempArray+length;++q) { // 遍歷數組初始化 *q = -1; } for (int j = 0; j < length; j++) { if (tempArray[numbers[j]] == -1) tempArray[numbers[j]] = numbers[j]; else if(tempArray[numbers[j]]== numbers[j]) { //曾被賦值一次或多次 *duplication = numbers[j]; //解引用 delete [] tempArray; return true; } } delete [] tempArray; //刪除指向數組的指針,不可省略方括號 return false; } // ====================測試代碼==================== bool contains(int array[], int length, int number) { for (int i = 0; i < length; ++i) { //只要有一個符合,即返回True if (array[i] == number) return true; } return false; } void test(char* testName, int numbers[], int lengthNumbers, int expected[], int expectedExpected, bool validArgument) { printf("%s begins: ", testName); // expected[]; 重復結果 // expectedExpected; 重復數量 int duplication; //自定義函數重復數 bool validInput = duplicate(numbers, lengthNumbers, &duplication); if (validArgument == validInput) { //是否有重復 if (validArgument) { if (contains(expected, expectedExpected, duplication)) printf("Passed.\n"); else printf("FAILED.\n"); } else printf("Passed.\n"); } else printf("FAILED.\n"); } // 重復的數字是數組中最小的數字 void test1() { int numbers[] = { 2, 1, 3, 1, 4 }; int duplications[] = { 1 }; test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); } // 重復的數字是數組中最大的數字 void test2() { int numbers[] = { 2, 4, 3, 1, 4 }; int duplications[] = { 4 }; test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); } // 數組中存在多個重復的數字 void test3() { int numbers[] = { 2, 4, 2, 1, 4 }; int duplications[] = { 2, 4 }; test("Test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); } // 沒有重復的數字 void test4() { int numbers[] = { 2, 1, 3, 0, 4 }; int duplications[] = { -1 }; // not in use in the test function test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false); } // 沒有重復的數字 void test5() { int numbers[] = { 2, 1, 3, 5, 4 }; int duplications[] = { -1 }; // not in use in the test function test("Test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false); } // 無效的輸入 void test6() { int* numbers = nullptr; int duplications[] = { -1 }; // not in use in the test function test("Test6", numbers, 0, duplications, sizeof(duplications) / sizeof(int), false); } void main() { test1(); test2(); test3(); test4(); test5(); test6(); getchar(); }

查找數組中重復的數字