跳转至

面试题 53 - I. 在排序数组中查找数字 I

题目描述

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

 

示例 1:

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

示例 2:

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

 

提示:

  • 0 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • nums 是一个非递减数组
  • -109 <= target <= 109

 

注意:本题与主站 34 题相同(仅返回值不同):https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/

解法

方法一:二分查找

由于数组 nums 已排好序,我们可以使用二分查找的方法找到数组中第一个大于等于 target 的元素的下标 $l$,以及第一个大于 target 的元素的下标 $r$,那么 target 的个数就是 $r - l$。

时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。

1
2
3
4
5
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        l = bisect_left(nums, target)
        r = bisect_right(nums, target)
        return r - l
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public int search(int[] nums, int target) {
        int l = lowerBound(nums, target);
        int r = lowerBound(nums, target + 1);
        return r - l;
    }

    private int lowerBound(int[] nums, int x) {
        int l = 0, r = nums.length;
        while (l < r) {
            int mid = (l + r) >>> 1;
            if (nums[mid] >= x) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }
}
1
2
3
4
5
6
7
8
class Solution {
public:
    int search(vector<int>& nums, int target) {
        auto l = lower_bound(nums.begin(), nums.end(), target);
        auto r = upper_bound(nums.begin(), nums.end(), target);
        return r - l;
    }
};
1
2
3
4
5
func search(nums []int, target int) int {
    l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
    r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
    return r - l
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
impl Solution {
    pub fn search(nums: Vec<i32>, target: i32) -> i32 {
        let search = |x| {
            let mut l = 0;
            let mut r = nums.len();
            while l < r {
                let mid = l + (r - l) / 2;
                if nums[mid] >= x {
                    r = mid;
                } else {
                    l = mid + 1;
                }
            }
            l as i32
        };
        search(target + 1) - search(target)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
    const search = x => {
        let l = 0;
        let r = nums.length;
        while (l < r) {
            const mid = (l + r) >> 1;
            if (nums[mid] >= x) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    };
    const l = search(target);
    const r = search(target + 1);
    return r - l;
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
    public int Search(int[] nums, int target) {
        int l = search(nums, target);
        int r = search(nums, target + 1);
        return r - l;
    }

    private int search(int[] nums, int x) {
        int l = 0, r = nums.Length;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (nums[mid] >= x) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }
}

评论