劍指offer--16.數組中重復的數字
阿新 • • 發佈:2019-03-20
class 指數 com ++ tle duplicate n) cat -- 時間限制:1秒 空間限制:32768K 熱度指數:198342
本題知識點: 數組
題目描述
在一個長度為n的數組裏的所有數字都在0到n-1的範圍內。 數組中某些數字是重復的,但不知道有幾個數字是重復的。也不知道每個數字重復幾次。請找出數組中任意一個重復的數字。 例如,如果輸入長度為7的數組{2,3,1,0,2,5,3},那麽對應的輸出是第一個重復的數字2。class Solution { public: // Parameters: // numbers: an array of integers // length: the length of array numbers// duplication: (Output) the duplicated number in the array number // Return value: true if the input is valid, and there are some duplications in the array number // otherwise false bool duplicate(int numbers[], int length, int* duplication) { for (inti=0; i<length; i++) { cout<<numbers[i]<<endl; if(count(numbers, numbers+length, numbers[i]) > 1) { *duplication = numbers[i]; return true; } } return false; } };
劍指offer--16.數組中重復的數字