Leetcode 33. Search in Rotated Sorted Array (python+cpp)

这篇博客介绍了如何解决LeetCode第33题——搜索旋转排序数组。博主通过二分查找的方法来解决此问题,并提供了Python和C++两种语言的代码实现。
摘要由CSDN通过智能技术生成

Leetcode 33. Search in Rotated Sorted Array

题目

在这里插入图片描述

解法:二分查找

直接上leetcode官方解释吧,挺清楚的
在这里插入图片描述
在这里插入图片描述
python代码:

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        start, end = 0, len(nums) - 1
        while start <= end:
            mid = start + (end - start) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] >= nums[start]:
                if target >= nums[start] and target < nums[mid]:
                    end = mid - 1
                else:
                    start = mid + 1
            else:
                if target <= nums[end] and target > nums[mid]: 
                    start = mid + 1
                else:
                    end = mid - 1
        return -1

参考链接

C++版本

class Solution {
public:
    int search(vector<int>& nums, int target) {
        // the idea is to search which sorted part does the target fall into, once we found this, the rest is a regular binary search because part of the condition will be always true
        // due to the special case of this problem, we need to check for the first and lst position
        if(nums[0] == target){
            return 0;
        }
        if(nums[nums.size() - 1] == target){
            return nums.size() - 1;
        }
        int l = 0, r = nums.size()-1;
        int mid;
        while(l+1 < r){
            mid = (r - l)/2 + l;
            // if nums[mid] > nums[0], from position 0 to mid is sorted
            if(nums[mid] > nums[0]){
                // if nums[mid] > target and nums[0] < target, then the target falls into a sorted range from position 0 to mid, then in the continue process, "nums[0] < target" will be always valid, it shrink to a regular binary search process
                if(nums[mid] > target && nums[0] < target){
                    r = mid;
                // Otherwise, the target is in the unsorted mid to end, then we change the left and continue searching for sorted part
                }else{
                    l = mid;
                }
            // if nums[mid] < nums[0], from position mid to end is sorted
            }else{
                // similarly, if nums[mid] < target and nums[end] > target, then the target falls into a sorted range from position mid to end, then in the continue process, "nums[end] > target" will be always valid, it shrink to a regular binary search process
                if(nums[mid] < target && nums[nums.size() - 1] > target){
                    l = mid;
                }else{
                    r = mid;
                }
            }
        }
        if(nums[l] == target){
            return l;
        }
        if(nums[r] == target){
            return r;
        }
        return -1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值