当前位置:网站首页>Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides

Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides

2022-06-13 05:38:00 Flying it people

The first one is : Dichotomy treatment , It is applicable to the lack of one data and the large amount of data :

public static double searchOne(int [] nums){

        int L = 0;
        int R = nums.length -1 ;
        double lowNum = 0;
        while (L<R){
            // Normal data 
            double middle = (nums[R] + nums[L])/2d;
            double nowMiddle= 0L;
            if(((R - L) + 1) % 2 == 0){
                // even numbers 
                nowMiddle = (nums[(R - L)/2 + L] + nums [(R - L)/2 + 1 + L])/2d;
            }
            else {
                // Odd number 
                nowMiddle = nums[(R - L )/2 + L];
            }
            if(nowMiddle == middle){
                lowNum = middle;
                L = L + 1 ;
                R = R - 1;
            }
            else  if (middle > nowMiddle){
                L = (R - L)/2 + L;
            }
            else {
                R = (R - L) / 2 ;
            }
        }
        return lowNum;
    }

The second kind : Suitable for less data , Lack of partial data , For example, find out the smallest or largest number in the real number :

public static List<Integer> searchMore(int [] nums){
        int R = nums.length - 1;
        List<Integer> list = new ArrayList<>();

        if(nums[R]  == R ){
            // No missing value 
        }
        else {
            for (int i = 0 ;i<nums.length ;i++){
                if(i != nums[i] - list.size()){
                    list.add(i + list.size());
                }
            }
        }
        return list;
    }

 

原网站

版权声明
本文为[Flying it people]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280509014450.html