[Dichotomy] The number of times the number appears in the ascending array

Title description

Count the number of times a number appears in the ascending array.
Example 1
Input
[1,2,3,3,3,3,4,5], 3
returns the value
4


Seeing that the array is ordered and searching, you should think of using dichotomy. The easiest way is to use dichotomy to find the number, and then use it as the center to search and count to the left and right sides.

public class Solution {
    
    
    public int GetNumberOfK(int[] array, int k) {
    
    
        // 二分查找
        int low = 0, high = array.length - 1, middle;
        int count = 0;
        while (low <= high) {
    
    
            middle = (low + high) / 2;
            if (k > array[middle])
                low = middle + 1;
            else if (k < array[middle])
                high = middle - 1;
            else {
    
    
                // find k
                int i = middle;
                while (i >= 0 && array[i] == k) {
    
    
                    count++;
                    i--;
                }
                i = middle + 1;
                while (i < array.length && array[i] == k) {
    
    
                    count++;
                    i++;
                }
                break;
            }
        }
        return count;
    }
}

The optimization method is to use the dichotomy to find the position of the first appearance and the last appearance of k, and subtract them to get the number of appearances of k. In order to get the position of the first appearance and the position of the last appearance, the dichotomy needs to be rewritten slightly.

First appearance
When array[middle] == k, let high = middle-1, continue to find the first position, and finally return to low

int low = 0, high = array.length - 1, middle = (low + high) / 2;
// get index of the first k.
while (low <= high) {
    
    
    middle = (low + high) / 2;
    if (k > array[middle]) {
    
    
        low = middle + 1;
    }
    // array[middle] == k 时,令high = middle - 1,继续往前找第一个出现的位置
    else {
    
    
        high = middle - 1;
    }
}
return low;

Last appearance
When array[middle] == k, let low = middle + 1, continue to find the last position, and finally return to high

int low = 0, high = array.length - 1, middle = (low + high) / 2;
// get index of the last k.
while (low <= high) {
    
    
    middle = (low + high) / 2;
    // array[middle] == k 时,令low = middle + 1,继续往后找最后一个出现的位置
    if (k >= array[middle]) {
    
    
        low = middle + 1;
    } else {
    
    
        high = middle - 1;
    }
}
return high;

Total code

public class Solution {
    
    
    public int GetNumberOfK(int[] array, int k) {
    
    
        // 二分查找
        int first = getK(array, k, true);
        int last = getK(array, k, false);
        System.out.println(first + " " + last);
        return last - first + 1;
    }

    public int getK(int[] array, int k, boolean first) {
    
    
        int low = 0, high = array.length - 1, middle = (low + high) / 2;
        if (first) {
    
    
            // get index of the first k.
            while (low <= high) {
    
    
                middle = (low + high) / 2;
                if (k > array[middle]) {
    
    
                    low = middle + 1;
                }
                // array[middle] == k 时,令high = middle - 1,继续往前找第一个出现的位置
                else {
    
    
                    high = middle - 1;
                }
            }
            return low;
        } else {
    
    
            // get index of the last k.
            while (low <= high) {
    
    
                middle = (low + high) / 2;
                // array[middle] == k 时,令low = middle + 1,继续往后找最后一个出现的位置
                if (k >= array[middle]) {
    
    
                    low = middle + 1;
                } else {
    
    
                    high = middle - 1;
                }
            }
            return high;
        }
    }

    public static void main(String[] args) {
    
    
        int[] num = {
    
    1, 2, 3, 3, 3, 3, 4, 5};
        new Solution().GetNumberOfK(num, 6);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43486780/article/details/113797002