题目一:找出数组中重复的数字。
。。。。。。S2
。。。。。
1 class Solution { 2 public: 3 // Parameters: 4 // numbers: an array of integers 5 // length: the length of array numbers 6 // duplication: (Output) the duplicated number in the array number 7 // Return value: true if the input is valid, and there are some duplications in the array number 8 // otherwise false 9 bool duplicate(int numbers[], int length, int* duplication) { 10 if (numbers == nullptr || length <= 0){ 11 return false; 12 } 13 14 15 for (int i = 0; i < length;i++){ 16 if (numbers[i] < 0 || numbers[i] >= length){ 17 return false; 18 } 19 } 20 21 22 for (int i=0; i < length; i++){ 23 while(numbers[i] != i){ 24 if ( numbers[i] == numbers[numbers[i]]){ 25 *duplication = numbers[i]; 26 return true; 27 } 28 int temp; 29 temp = numbers[numbers[i]]; 30 numbers[numbers[i]] = numbers[i]; 31 numbers[i] = temp; 32 } 33 } 34 return false; 35 } 36 };
原文地址:https://www.cnblogs.com/shiganquan/p/9280891.html
时间: 2024-11-03 01:22:22