剑指offer(第二版)——数字在排序数组中出现的次数

PS:《剑指offer》是很多同学找工作都会参考的一本面试指南,同时也是一本算法指南(为什么它这么受欢迎,主要应该是其提供了一个循序渐进的优化解法,这点我觉得十分友好)。现在很多互联网的算法面试题基本上可以在这里找到影子,为了以后方便参考与回顾,现将书中例题用Java实现(第二版),欢迎各位同学一起交流进步。

GitHub: https://github.com/Uplpw/SwordOffer

剑指offer完整题目链接: https://blog.youkuaiyun.com/qq_41866626/article/details/120415258

1 题目描述

统计一个数字在排序数组中出现的次数。

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: 2

leetcode链接: 数字在排序数组中出现的次数(以下代码已测试,提交通过)

2 测试用例

一般是考虑功能用例,特殊(边缘)用例或者是反例,无效测试用例这三种情况。甚至可以从测试用例寻找一些规律解决问题,同时也可以让我们的程序更加完整鲁棒。

(1)功能用例:数组包含目标值,数组不包含目标值。

(2)边缘用例:无。

(3)无效用例:数组为空。

3 思路

分析:

题目比较简单,直接介绍一些解法。

解法1:暴力求解

遍历数组,统计目标值出现的次数。

解法2:利用二分

看到有序数组,一般都可以采用二分思想。

不过本题利用二分思想,也有不同的代码写法。

  • 第一种是先用二分找到目标值索引,然后往前往后遍历计数,直到不等;如果找不到直接返回。(这种写法效率不如第二种,不过感觉很直观)

  • 第二种直接求目标值的左右边界,然后右边界减去左边界即可

4 代码

算法实现:

public class NumberOfK {
    // 解法1:暴力求解
    public static int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int count = 0;
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (nums[i] == target) {
                count++;
            }
            if (nums[i] > target) {
                break;
            }
        }
        return count;
    }

    // 解法2:二分第一种方式
    public static int search1(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int count = 0;
        int index = getIndex(nums, target);
        if (index == -1) {
            return count;
        } else {
            count++;
            int left = index - 1;
            int right = index + 1;
            while (left >= 0) {
                if (nums[left] == target) {
                    count++;
                    left--;
                } else {
                    break;
                }
            }
            while (right <= nums.length - 1) {
                if (nums[right] == target) {
                    count++;
                    right++;
                } else {
                    break;
                }
            }
        }
        return count;
    }

    public static int getIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        int index = -1;
        // 注意结束条件
        while (left <= right) {
            // mid =  (left + right) / 2 可能出现向上溢出
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                index = mid;
                break;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        return index;
    }

    // 解法2:二分第二种方式,右边界-左边界
    public static int search2(int[] nums, int target) {
        return search2Core(nums, target) - search2Core(nums, target - 1);
    }

    public static int search2Core(int[] nums, int target) {
        int i = 0, j = nums.length - 1;
        while (i <= j) {
            // 注意溢出
            int m = i + (j - i) / 2;
            if (nums[m] <= target) i = m + 1;
            else j = m - 1;
        }
        return i;
    }

    public static void main(String[] args) {
        int[] nums = {5, 7, 7, 8, 8, 10};
        int[] nums1 = {1};
        System.out.println(search(nums, 8));
        System.out.println(search1(nums, 8));
        System.out.println(search2(nums, 8));
    }
}

参考
在解决本书例题时,参考了一些大佬的题解,比如leetcode上的官方、K神,以及其他的博客,在之后的每个例题详解后都会给出参考的思路或者代码链接,同学们都可以点进去看看!

本例题参考:

  1. K神题解

本文如有什么不足或不对的地方,欢迎大家批评指正,最后希望能和大家一起交流进步、拿到心仪的 offer !!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值