Java implements the number of times a number appears in a sorted array

Count the number of times a number appears in a sorted array

code

Solution one

    /**
     * Violent solution, direct traversal
     * @param array
     * @param k
     * @return
     */
    public static int findAppearCountInArrayOfK(int[] array, int k) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == k) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 3, 3, 3};
        int c = findAppearCountInArrayOfK(array, 3);
        System.out.print(c);
    }

Solution two

    /**
     * Based on binary search, find the start and end positions respectively, and find the difference
     * @param array
     * @param k
     * @return
     */
    public static int findAppearCountInArrayOfK2(int[] array, int k) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int first = findFirstIndexOfK(array, k);
        int last = findLastIndexOfK(array, k);
        if (first > -1 && last > -1) {
            System.out.println(first + " - " + last);
            return last - first + 1;
        }
        return 0;
    }

    /**
     * Find the starting position of k
     * @param array
     * @param k
     * @return
     */
    private static int findFirstIndexOfK(int[] array, int k) {
        int low = 0;
        int high = array.length - 1;
        int middle = 0;
        int middleData = 0;
        while (low <= high) {
            // Calculate midpoint
            middle = (low + high) / 2;
            // get the median
            middleData = array[middle];
            if (middleData == k) {
                // if the median is equal to the target value
                // At this time, if the midpoint is exactly the starting position of the array, indicating that there is no element in front of it, then return to this position directly
                // If there is an element in front and it is not equal to the target value, it means that the current position is the starting position of the target value, and returns directly
                // If neither is satisfied, it means that it is not found, then modify the high of the array search range, take the midpoint of the current array as the boundary, and binary search for the left sub-array
                if (middle == 0 || array[middle - 1] != k) {
                    return middle;
                } else {
                    high = middle - 1;
                }
            } else if (middleData > k) {
                // If the median is greater than k, it means that k is in the left subarray, continue to the next binary search
                high = middle - 1;
            } else {
                // If the median is less than k, it means that k is in the subarray on the right, continue to the next binary search
                low = middle + 1;
            }
        }
        return -1;
    }

    private static int findLastIndexOfK(int[] array, int k) {
        int low = 0;
        int high = array.length - 1;
        int middle = 0;
        int middleData = 0;
        while (low <= high) {
            middle = (low + high) / 2;
            middleData = array[middle];
            if (middleData == k) {
                // if the median is equal to the target value
                // At this time, if the midpoint is exactly the end position of the array, indicating that there are no elements behind, then return to this position directly
                // If there is an element behind and it is not equal to the target value, it means that the current position is the end position of the target value, and returns directly
                // If neither is satisfied, it means that it is not found, then modify the low of the array search range, take the midpoint of the current array as the boundary, and binary search for the right sub-array
                if (middle == array.length - 1 || array[middle + 1] != k) {
                    return middle;
                } else {
                    low = middle + 1;
                }
            } else if (middleData > k) {
                // If the median is greater than k, it means that k is in the left subarray, continue to the next binary search
                high = middle - 1;
            } else {
                // If the median is less than k, it means that k is in the subarray on the right, continue to the next binary search
                low = middle + 1;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        int[] array = {1, 2, 3, 3, 3, 3};
        int c = findAppearCountInArrayOfK2(array, 3);
        System.out.print(c);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939850&siteId=291194637