Title One: Find the duplicate numbers in the array.
。。。。。。 S2
。。。。。
1 classSolution {2 Public:3 //Parameters:4 //numbers:an array of integers5 //length:the length of array numbers6 //duplication: (Output) The duplicated number in the array number7 //Return Value:true If the input is valid, and there be some duplications in the array number8 //otherwise false9 BOOLDuplicateintNumbers[],intLengthint*duplication) {Ten if(numbers = = nullptr | | length <=0){ One return false; A } - - the for(inti =0; I < length;i++){ - if(Numbers[i] <0|| Numbers[i] >=length) { - return false; - } + } - + A for(intI=0; i < length; i++){ at while(Numbers[i]! =i) { - if(Numbers[i] = =Numbers[numbers[i]]) { -*duplication =Numbers[i]; - return true; - } - inttemp; intemp =Numbers[numbers[i]]; -Numbers[numbers[i]] =Numbers[i]; toNumbers[i] =temp; + } - } the return false; * } $};
"Sword means offer" 3, repeated numbers in the array