xinyu04

导航

LeetCode 33 Search in Rotated Sorted Array 二分

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index \(k\) (\(1 <= k < nums.length\)) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (\(0\)-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index \(3\) and become [4,5,6,7,0,1,2].

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or \(-1\) if it is not in nums.

You must write an algorithm with \(O(\log n)\) runtime complexity.

Solution

我们可以发现数组的两段分别是有序的,所以依然可以用二分来解决。

点击查看代码
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        int l = 0, r = n-1;
        while(l<=r){
            int mid = (l+r)>>1;
            if(nums[mid]==target) return mid;
            if(nums[mid]<nums[r]){
                if(nums[mid]<target && target<=nums[r]) l = mid+1;
                else r = mid-1;
            }
            else{
                // nums[mid]>nums[r]
                if(nums[l]<=target && target<nums[mid]) r = mid-1;
                else l = mid+1;
            }
        }
        return -1;
    }
};

posted on 2022-07-28 22:20  Blackzxy  阅读(20)  评论(0编辑  收藏  举报