五月集训 DAY09【二分查找】

35.搜索插入位置

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。

输入: nums = [1,3,5,6], target = 5
输出: 2

输入: nums = [1,3,5,6], target = 2
输出: 1

输入: nums = [1,3,5,6], target = 7
输出: 4

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums 为 无重复元素 的 升序 排列数组
  • -104 <= target <= 104
int searchInsert(int* nums, int numsSize, int target){
    int left = 0;
    int right = numsSize;
    int mid;
    if(nums[right-1] < target){
        return right;
    }
    if(nums[0] >= target){
        return 0;
    }
    while(left ^ right) {
        //左右指针相等跳出循环
        mid = (left + right) >> 1;
        if(target <= nums[mid]){
            right = mid;
        }
        if(nums[mid] <= target){
            left = mid;
        }
        if(right - left == 1){
            break;
        }
    }
    return right;
}

解题思路:
(1)分两种情况,要么在数组中间插入,要么在数组的内部插入
(2)在数组内部插入的时候,让left和rihgt都满足查找的条件,不断缩小它们之间的距离。
(3)距离为0,或者距离为1 的时候就跳出循环

在这里插入图片描述

704.二分查找

给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。

输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4

输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1

  • 你可以假设 nums 中的所有元素是不重复的。
  • n 将在 [1, 10000]之间。
  • nums 的每个元素都将在 [-9999, 9999]之间。
int search(int* nums, int numsSize, int target){
    int l = 0; int m = 0; 
    int r = numsSize;
    int ret = r - l;
    while(1){
        m = (l+r) >> 1;
        ret = r - l;
        if(nums[m] <= target){
            l = m;
        }
        if(target <= nums[m]){
            r = m;
        }
        if(ret == 0){
        return r;           					//(1)
        }
        if(ret == 1 && nums[m] != target){
        return -1;								//(2)
        }
    }
}

解题思路:
(1)如果ret为0,说明左游标等于右游标,能够找到target。
(2)如果ret的值为1,而且这个值不等于目标值target,说明没找到,返回-1。

剑指Offer 53-I 在排序数组中查找数字I

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

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

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

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • nums 是一个非递减数组
  • -10^9 <= target <= 10^9
int search(int* nums, int numsSize, int target){
    int l = 0;
    int r = numsSize - 1;
    if(numsSize == 1 && nums[0] == target){
        return 1;
    }
    while(l < r){
        if(nums[l] != target){
            l++;
        }
        if(nums[r] != target){
            r--;
        }
        if(nums[l] == target && nums[r] == target){
            return r-l+1;
        }
    }
    return 0;
}

解题思路:
(1)左游标递增找下标最小的target,右游标递减找下标最大的target
(2)都找到之后,r - l + 1就是target的数目
(3)注意数组长度为1时候的特殊情况。

911.在线选举

给你两个整数数组 persons 和 times 。在选举中,第 i 张票是在时刻为 times[i] 时投给候选人 persons[i] 的。
对于发生在时刻 t 的每个查询,需要找出在 t 时刻在选举中领先的候选人的编号。
在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。
实现 TopVotedCandidate 类:
TopVotedCandidate(int[] persons, int[] times) 使用 persons 和 times 数组初始化对象。
int q(int t) 根据前面描述的规则,返回在时刻 t 在选举中领先的候选人的编号。

输入:
[“TopVotedCandidate”, “q”, “q”, “q”, “q”, “q”, “q”]
[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
输出:
[null, 0, 1, 1, 0, 0, 1]
解释:
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // 返回 0 ,在时刻 3 ,票数分布为 [0] ,编号为 0 的候选人领先。
topVotedCandidate.q(12); // 返回 1 ,在时刻 12 ,票数分布为 [0,1,1] ,编号为 1 的候选人领先。
topVotedCandidate.q(25); // 返回 1 ,在时刻 25 ,票数分布为 [0,1,1,0,0,1] ,编号为 1 的候选人领先。(在平局的情况下,1 是最近获得投票的候选人)。
topVotedCandidate.q(15); // 返回 0
topVotedCandidate.q(24); // 返回 0
topVotedCandidate.q(8); // 返回 1

  • 1 <= persons.length <= 5000
  • times.length == persons.length
  • 0 <= persons[i] < persons.length
  • 0 <= times[i] <= 10^9
  • times 是一个严格递增的有序数组
  • times[0] <= t <= 10^9
  • 每个测试用例最多调用 10^4 次 q

解题思路:
(1)根据times数组构建不同时刻选举人票数的数组,t时刻可以访问到每个人的票数。
(2)t还能直接得到上一个得到选票的人是谁,bool,谁得到选票,他为1,另一个为0。
(3)比较票数,返回选举人编号,如果票数相等,再根据bool返回选举人编号。

class TopVotedCandidate {
public:
    TopVotedCandidate(vector<int>& persons, vector<int>& times) {
        int i = 0;
        for(i = 0; i < times->size(); i++){
            q(*(times+i));
        }
    }
    
    int q(int t) {
        
    }
};

/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj->q(t);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值