Offer an array prove safety of repeated numbers

Title Description

All numbers in a length of n in the array are in the range 0 to n-1. Some digital array is duplicated, but do not know how many numbers are duplicated. Do not know each digit is repeated several times. Please find an array of any one of the duplicate numbers. For example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output of the first 2 repeating digits.

Thinking

  1. Violent solution, traverse, find duplicate and repeated returns true digital
  2. Hash, since the number of 1 ~ n-1, may be established hash array holds the number to each number appears, if the number exceeds 2, the repeated description
  3. Use of the subject properties, n-digits, less than n, the number can be restored to a position corresponding to the numerals, i.e., return to i i-th digital position, if found i = Numbers [i], duplication, you can return

Code

method one:

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) {
        if(length < 1)
            return false;
        for(int i = 0; i < length; i++)
        {
            for(int j = i+1;j<length;j++)
            {
                if(numbers[i] == numbers[j]){
                    *duplication = numbers[i];
                    return true;
                }
                    
            }
        }
        return false;
    }
};

Method Two:

class Solution {
public:
    bool duplicate(int numbers[], int length, int* duplication) {
        if(length < 1)
            return false;
        vector<int> Hash(length);
        for(int i = 0; i < length;i++)
        {
            Hash[numbers[i]]++;
        }
        for(int i = 0; i < length;i++)
        {
            if(Hash[i]>1)
            {
                *duplication = i;
                return true;
            }
        }
        return false;
    }
};

Method three:

class Solution {
public:
    bool duplicate(int numbers[], int length, int* duplication) {
        if(length < 1)
            return false;
        int index = 0;
        while(index < length)
        {
            if(numbers[index]== index)
            {
                index ++;
            }
            else
            {
                int tmp = numbers[index];
                if(tmp == numbers[tmp]) // 出现重复
                {
                    *duplication = tmp;
                    return true;
                }
                else //没有重复,则换位置
                {
                    numbers[index] = numbers[tmp];
                    numbers[tmp] = tmp;
                }
            }
        }
        return false;
    }
};
Published 85 original articles · won praise 0 · Views 407

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104744970