首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 33. 搜索旋转排序数组

LeetCode 33. 搜索旋转排序数组

原创
作者头像
freesan44
修改于 2021-10-18 08:08:53
修改于 2021-10-18 08:08:53
3080
举报
文章被收录于专栏:freesan44freesan44

题目地址(33. 搜索旋转排序数组)

https://leetcode-cn.com/problems/search-in-rotated-sorted-array/

题目描述

代码语言:txt
AI代码解释
复制
整数数组 nums 按升序排列,数组中的值 互不相同 。

在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。

给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。

 

示例 1:

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


示例 2:

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

示例 3:

输入:nums = [1], target = 0
输出:-1


 

提示:

1 <= nums.length <= 5000
-10^4 <= nums[i] <= 10^4
nums 中的每个值都 独一无二
题目数据保证 nums 在预先未知的某个下标上进行了旋转
-10^4 <= target <= 10^4

 

进阶:你可以设计一个时间复杂度为 O(log n) 的解决方案吗?

思路

二分法,查找到左右两边哪边是正常序列,然后查找target是否在正常序列里面,如果不在就定位一边

代码

  • 语言支持:Python3

Python3 Code:

代码语言:txt
AI代码解释
复制
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left, right = 0, len(nums)-1
        while left <= right:
            mid = (left + right)//2
            midNum = nums[mid]
            print(left,right,mid)
            if midNum == target:
                return mid
            #正常序列在左边
            if midNum >= nums[left]:
                if nums[left] <= target < midNum:
                    right = mid - 1
                else:
                    left = mid + 1
            else:#正常序列在右边时
                if midNum < target <= nums[right]:
                    left = mid + 1
                else:
                    right = mid -1
        return -1

复杂度分析

令 n 为数组长度。

  • 时间复杂度:$O(logn)$
  • 空间复杂度:$O(1)$

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode - #33 搜索旋转排序数组(Top 100)
我们社区陆续会将顾毅(**Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤道长[1]**)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。
Swift社区
2022/04/04
4320
LeetCode - #33 搜索旋转排序数组(Top 100)
33. 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
韩旭051
2020/06/23
2660
33. 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
lucifer210
2019/09/29
4390
33. 搜索旋转排序数组
leetcode刷题(73)——33. 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
老马的编程之旅
2022/06/22
1870
leetcode刷题(73)——33. 搜索旋转排序数组
LeetCode-33 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7]可能变为 [4,5,6,7,0,1,2] )。
用户3470542
2019/06/26
1.3K0
LeetCode-33 搜索旋转排序数组
【刷穿 LeetCode】33. 搜索旋转排序数组(中等)
例如, [0,1,2,4,5,6,7] 经旋转后可能变为 [4,5,6,7,0,1,2] 。
宫水三叶的刷题日记
2021/02/20
2860
33. 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
张伦聪zhangluncong
2022/10/26
1830
☆打卡算法☆LeetCode 33、搜索旋转排序数组 算法解析
“给定一个旋转后的数组和整数target,如果数组中存在整数target,则返回它的下标。”
恬静的小魔龙
2022/08/07
2220
☆打卡算法☆LeetCode 33、搜索旋转排序数组  算法解析
LeetCode题目33:搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
二环宇少
2020/08/13
5200
LeetCode题目33:搜索旋转排序数组
33. 搜索旋转排序数组
( 例如,数组 0,1,2,4,5,6,7 可能变为 4,5,6,7,0,1,2 )。
Michel_Rolle
2021/03/02
3.3K0
LeetCode-33-搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
benym
2022/07/14
2680
打卡群2刷题总结1003——搜索旋转排序数组
https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
木又AI帮
2020/10/10
4970
Leetcode No.33 搜索旋转排序数组(二分法)
升序排列的整数数组 nums 在预先未知的某个点上进行了旋转(例如, [0,1,2,4,5,6,7] 经旋转后可能变为 [4,5,6,7,0,1,2] )。
week
2022/01/07
1970
Leetcode No.33 搜索旋转排序数组(二分法)
T11-搜索旋转排序数组
今天分享leetcode第11篇文章,也是leetcode第33题—Search in Rotated Sorted Array(搜索旋转排序数组),地址是:https://leetcode.com/problems/search-in-rotated-sorted-array/
木又AI帮
2019/07/18
3980
T11-搜索旋转排序数组
搜索旋转排序数组(leetcode33)
在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
Vincent-yuan
2022/05/06
2820
搜索旋转排序数组(leetcode33)
【leetcode刷题】20T18-搜索旋转排序数组
https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
木又AI帮
2020/02/25
2860
33. 搜索旋转排序数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
名字是乱打的
2021/12/23
2630
33. 搜索旋转排序数组
【奇技淫巧】-- 搜索旋转数组
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
看、未来
2020/08/26
3040
【奇技淫巧】-- 搜索旋转数组
搜索旋转排序数组(leetcode 33)
如数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]。
恋喵大鲤鱼
2023/10/12
2240
搜索旋转排序数组(leetcode 33)
LeetCode 33. 搜索旋转排序数组(二分查找)
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
Michael阿明
2020/07/13
3290
LeetCode 33. 搜索旋转排序数组(二分查找)
相关推荐
LeetCode - #33 搜索旋转排序数组(Top 100)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档