leetcode第十五周解题总结--二分查找(二)

这两题是上周几道题的结合。


33. Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.

题意解析: 在旋转数组中进行目标值搜索,如果找到返回索引,没有则返回-1。

解题思路: 虽然是上次两题的结合题,但是复杂度明显增加了许多,需要对于多种情况进行考虑。

l = 0,r = n -1
m:中间索引

  1. 当中间值大于等于最左值,则中间值属于左半段
    • 当目标值大于等于最左值,小于中间值,那么目标值肯定在最左值和中间值(l,m - 1)之间。
    • 否则,目标值在中间值和最右值(m + 1, r)之间
  2. 当中间值小于等于最右值,则中间值属于右半段
    • 当目标值小于等于最右值,大于中间值,那么目标值肯定在中间值和最右值(m + 1,r)之间。
    • 否则,目标值在最左值和中间值(l,m - 1)之间
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int l = 0;
        int r = nums.size() -1;
        if (l==r) return nums[l] == target? l:(-1);
        while(l <= r) {
            int m = (l + r) /2;
            if(nums[m] == target) return m;
            if(nums[m]>=nums[l]) {
                if(target < nums[m] && target>=nums[l]){
                    r = m - 1;
                }else {
                    l = m + 1;
                }
            }

            if(nums[m]<=nums[r]) {
                if(target <= nums[r] && target > nums[m]){
                    l = m + 1;
                }else {
                    r = m - 1;
                }

            }

        }
        return -1;
    }
};

可以适用于没有旋转的数组,即中间值一直大于等于最左值,相当于认为一直落在左半段。

81. Search in Rotated Sorted Array II

Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?

Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

题意解析: 在旋转数组中进行目标值搜索,加上条件,存在重复值,并且改成true或者false的存在命题。

所以为什么上周的同意寻找最小值是hard难度,这个搜索目标值是medium???

解题思路:
和上周的思路类似,遇到相同的情况,就使得最左值右移一位,越过重复值。

分成三种情况:

  • 中间值大于最左值,左半段
  • 中间值小于最左值,右半段
  • 中间值等于最左值,右移一位
class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int n = nums.size();
        if(n == 0) return false;

        int l = 0;
        int r = n - 1;
        if (l==r) return nums[l] == target? true:false;

        while(l <= r) {
            int m = (l + r) /2;
            if(nums[m] == target) return true;

            if(nums[m]>nums[l]) {
                if(target < nums[m] && target>=nums[l]){
                    r = m - 1;
                }else {
                    l = m + 1;
                }
            }else if(nums[m]<nums[l]) {
                if(target <= nums[r] && target > nums[m]){
                    l = m + 1;
                }else {
                    r = m - 1;
                }

            }else{
                l ++;
            }

        }
        return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值