码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [python 刷题] 33 Search in Rotated Sorted Array


    [python 刷题] 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.

    这道题也是一道 sorted array,和 [python 刷题] 153 Find Minimum in Rotated Sorted Array 的处理逻辑有点像,不过 [python 刷题] 153 Find Minimum in Rotated Sorted Array 只要找最小数,因此只要判断 nums[m] 和 nums[r] 的关系即可,这里的情况稍微复杂一些。

    图像上显示,一共会有两种情况:

    在这里插入图片描述

    在这里插入图片描述

    每个情况又会延伸出两个需要解决的对应情况,因此总共需要手动处理 4 种对应的情况

    1. 找出比图 1 中高亮的元素小的元素

      这个处理起来比想象中的稍微麻烦一些,因为比 9 小的值可能同时存在于两个 cluster 间:

      在这里插入图片描述

      因此这个时候还需要判断 target 是否比 nums[l] 小,如果比 nums[l] 小,那么就一定在右边的 cluster 中

    2. 找出比图 1 中高亮的元素大的元素

      这个情况比较简单,只要从右边的 cluster 中搜索即可

    3. 找出比图 2 中高亮的元素大的元素

      这个情况与情况 1 一样,也会同时坐落于两个区间:

      在这里插入图片描述

      也因此需要判断 target 和 nums[l] 之间的关系,如果 target 比 nums[l] 小,那么 target 一定坐落于右侧的 cluster

      反之 target 一定坐落于左侧的 cluster 中

    4. 找出比图 2 中高亮的元素小的元素

      这一定在 cluster 的左边

    一旦思路理清了,那么代码写起来也很容易了:

    class Solution:
        def search(self, nums: List[int], target: int) -> int:
            l, r = 0, len(nums) - 1
            while l <= r:
                m = (l + r) // 2
                if nums[m] == target:
                    return m
    
                if nums[m] < nums[l]:
                    # the it means there are 2 assending order in the curr subarray
                    if target < nums[m] or target > nums[r]:
                        r = m - 1
                    else:
                        l = m + 1
                else:
                    if target > nums[m] or target < nums[l]:
                        l = m + 1
                    else:
                        r = m - 1
    
            return -1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    另,如果刚开始的判断不是做 nums[m] < nums[l] 的话,记得要用 ≥ \ge ≥: if nums[m] >= nums[l]:

  • 相关阅读:
    spring5.0 源码解析(day06) initApplicationEventMulticaster();
    JavaWeb 七个步骤,完成一个servlet的hello world程序
    毕业论文使用的卡方检验该如何分析?
    2022锦江行——非繁城品:疫情之下,存量酒店的突围之道
    【Spring Cloud】深入理解 Eureka 注册中心的原理、服务的注册与发现
    硬件设计之——GPIO配置表
    nginx配置新的SSL证书后浏览器仍显示之前的旧SSL证书
    2022-09-06 mysql/stonedb-知识网格-直方图HISTs
    CSS案例-1.字体样式练习
    Linux操作系统~系统文件IO,什么是文件描述符fd?什么是vfs虚拟文件系统
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/133577930
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号