Title prove safety offer two: an array of digital repeated

Title II: an array of digital repeated

Title Description

in aLength nThe array of all the numbers are in0 to n-1In the range.
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

A thought: violence algorithm

Through all the data.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-puEeqQA1-1579235902262) (images / 02.png)]

/**
     *
     * @param numbers
     * @param length
     * @param duplication
     * @return
     * 29ms 9588k  时间复杂度O(n^2)
     *  // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false

     */
    public static  boolean duplicate1(int numbers[],int length,int [] duplication) {
        for(int i=0;i<length;i++){
            for(int j=i+1;j<length;j++){
                if(numbers[i]==numbers[j]){
                    duplication[0]=numbers[i];
                    return true;
                }
            }
        }
        return false;
    }

Thinking two: the first sort, then compare adjacent data

    /**
     *
     * @param numbers
     * @param length
     * @param duplication
     * @return
     * 思路二:将数组排序:排完序 比较相邻的数据
    		时间复杂度为 O(NLOGN)
     */
    public static  boolean duplicate2(int numbers[],int length,int [] duplication) {

        Arrays.sort(numbers);
        for(int i=0;i<length-1;i++){
            if(numbers[i]==numbers[i+1]){
                duplication[0]=numbers[i];
                return true;
            }
        }
        return false;
    }

Thinking three: Use HashSet

Thinking three: use HashSet to solve the problem
through the array, if that element exists in the HashSet is ended
if there is no element is added to the HashSet

Time complexity: O (n)

Space complexity: O (n)

    public static  boolean duplicate3(int numbers[],int length,int [] duplication){
		//定义HashSet
        HashSet<Integer> hashSet = new HashSet<>();
        for(int i=0;i<length;i++){
            if(hashSet.contains(numbers[i])){//在HashSet中存在该元素
                duplication[0]=numbers[i];
                return true;
            }else{  //在HashSet中不存在该元素
                hashSet.add(numbers[i]);
            }
        }
        return false;

    }

Ideas IV: A Sorting by location

We note numbers in the array are 0 ~ n-1,
If the array does not duplicate numbers, then after the sort the array position numbers will appear in the i position i
Since the array has duplicate numbers, there may be some of the plurality of digital position, while there may be some positions not available.

The idea four: from start to finish in order to scan the array.
When the subscript i to the scanning array, the first digital comparator (denoted by m) is not equal to i.
If so, then scan the next number,
if not, to take him and the m-th digit comparison. If it is equal and the m-th digit, to find a repeating number (this number at index i and m have appeared) and if it is not equal to the m-th digit, the i-th digit and put the m-th digital switching, m put into his position.
Then repeat the process more exchanges

Core: n array of n data if no repeat length, is bound may be sequentially discharged in order,

One option is actually sort; but this algorithm will destroy the structure of the array

Time complexity: O (n)

 public static  boolean duplicate4(int numbers[],int length,int [] duplication){

        for(int i =0;i<length;i++){//从头到尾扫描整个数组
            int value = numbers[i];
            while(value!=i){//当前这个数字不等于i
                //和第value个数字比较,如果相等就找到了一个重复数值
                if(numbers[value]==value){
                    duplication[0]=value;
                    return true;
                }else{//不相等,则把第i个数字和第value个数字交换
                    int temp = numbers[i];
                    numbers[i]=numbers[value];
                    numbers[value]=temp;
                }
            }

        }
            return  false;
    }

Ideas Five: Find based on the number of binary data

5 ideas: Why array duplicate data?

Assuming no repeat numbers, then the data within a range from 1 ~ n n digits only.

Now that the array contains more than n digits, so be sure to include duplicate numbers.

LooksThe number of digits within a rangeIt is important to solve this problem

We from a number n is divided into two parts from the intermediate numbers m, the front part 1 m, the rear part of the m + 1 ~ n

If the number of digits 1 ~ m m is more than half that which must contain duplicate numbers; otherwise, the other half contain duplicate numbers *

Interval we can continue it contains duplicate numbers into two, until you find a duplicate numbers.

This process is a binary search algorithm ruthless similar, but more than the number of step in the statistical range of numbers.

Time complexity: O (nLogn)

    public static  boolean duplicate5(int numbers[],int length,int [] duplication) {

        int start = 1;//可能的最小值
        int end = length;//可能的最大值
        int mid = (start+end)/2;//计算中值 便于二分查找


        while(end>=start) {
            //计算在 start - mid中间值出现的次数
            int count = countRange(numbers, length, start, mid);
            System.out.println(start+"---"+mid+"值出现的次数--"+count);

            if (start == end) { //首末值相等,一个数据
                if (count > 1) {  //假如count值大于1,说明改数据出现了不止一次
                    duplication[0]=start;
                    return true;
                } else {  //说明没有重复
                    break;
                }
            }
            if (count > mid - start+1) { //在start-mid中华间数据个数多了,则继续查找
                end = mid;
                mid = (start + end) / 2;
            } else {  //
                start = mid + 1;
                mid = (start + end) / 2;
            }

        }
        return false;
    }

    /**
     * 计算在数组中 start-end数据出现的次数
     * @param arr
     * @param lenght
     * @param start
     * @param end
     * @return
     */
    public  static  int countRange(int[] arr,int lenght,int start,int end){

        int  count = 0;
        for(int i=0;i<lenght;i++){//遍历数组,进行统计在start于end之间数据的个数
            if(arr[i]>=start&&arr[i]<=end){
                count++;
            }
        }
        return count;
    }

This algorithm is not guaranteed to find all duplicate data

Published 101 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/ZHOUJIAN_TANK/article/details/104017436