当前位置:网站首页>【LeetCode】33. 搜索旋转排序数组
【LeetCode】33. 搜索旋转排序数组
2022-06-12 22:17:00 【LawsonAbs】
1.题目
其实这题挺难的。
2.思想
拿到这道题时,我首先考虑的问题是:
- 找到数组的旋转分界线,然后这样我们就可以还原数组(得到一个有序的数组),然后根据二分查找再来迅速定位。
但问题是:是否有必要找到数组的旋转分界线? 其实是没有必要的。那么有没有其它的方法解决这个问题呢?
传统的二分类中,我们都使用mid 下标处的值作为判断依据,但是那是在一个完全有序的数组中是成立的,如果一个数组并非完全有序,那么还有什么其它的判断方法吗?例如本题中,题目给定的条件是:数组原本是有序的,经过一个旋转操作变成了无序,那么在查找的时候,我们可以知道其实 [left,mid-1],[mid,right]中必有一个是有序的数组。举个例子:在数组中[6,7,1,2,3,4,5],我们枚举任意一个下标的数,都会发现由mid划分成的两个区间满足至少有一个区间是有序的,根据这个特性,我们便可以得到一个复杂版的二分法。再分别在这两个子区间内做分析,问题就迎刃而解。
3.代码
可AC的代码如下:
class Solution:
# 得到
def search(self, nums: List[int], target: int) -> int:
left ,right = 0, len(nums)-1
while(left <= right):
mid = (left + right) // 2
if nums[mid] == target: # 这里先比较就避免了后面两次再比较
return mid
if left == mid:
left = mid+1
continue
# 至少有一端是有序的
# 先判断有几个数,左侧是否有序
if nums[left] <= nums[mid-1]:
if nums[left] <= target and target <=nums[mid-1]: # 在下面这个区间二分查找
right = mid-1
else: # 如果不在这个区间,则可能在另外一半儿
left = mid+1
else : # 右侧有序
if nums[mid] <= target and target <=nums[right]: # 在下面这个区间二分查找
left = mid+1
else: # 如果不在这个区间,则仍在左半儿
right = mid-1
return -1
下面这段代码是我的第一印象想法,
class Solution:
def search(self, nums: List[int], target: int) -> int:
bound_idx = get_bound(nums)
if bound_idx == 0:
else:
def get_bound(self,nums):
idx = 1
if len(nums) > 1 :
if nums[idx] > nums[idx-1]: # 前两个数满足升序
else:
return idx-1 # 直接返回0
nxt = idx << 1 # 左移一位
while(idx < len(nums) and nums[idx] < nums[nxt]):
idx = nxt
# 说明在区间 [idx,nxt] 中是存在一个
return idx # 返回下标
边栏推荐
- [Jianzhi offer simple] Jianzhi offer 06 Print linked list from end to end
- SQL tuning guide notes 14:managing extended statistics
- #yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
- SQL tuning guide notes 9:joins
- Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
- 2023届校园招聘正式开启!OceanBase 想和你在这个春天约一场面试
- Ansible foundation and common modules (I)
- Configuring Dingding notification of SQL audit platform archery
- [data analysis] data clustering and grouping based on kmeans, including Matlab source code
- The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
猜你喜欢

How to prevent phishing emails? S/mime certificate to help!

SQL tuning guide notes 9:joins

Ansible foundation and common modules (I)

Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)

PE安装win10系统

Flutter series part: detailed explanation of GridView layout commonly used in flutter

Recommended Chinese font in the code input box of Oracle SQL developer

Oracle livelabs experiment: introduction to Oracle Spatial

孙老师版本JDBC(2022年6月12日21:34:25)

Things about the kotlin collaboration process - pipeline channel
随机推荐
How to prevent phishing emails? S/mime certificate to help!
JVM Basics - > how to troubleshoot JVM problems in your project
[simple] 155 Minimum stack
生成小程序菊花码(生成菊花码、更换中间logo、更改图片尺寸,加文字)
Audio and video technology development weekly 𞓜 234
SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database
SQL tuning guide notes 13:gathering optimizer statistics
打新债开户安全么,新手该怎么操作?
3.5 测试类的setup和teardown
MySQL introduction and installation (I)
Is it safe to open an account with new bonds? How should novices operate?
June training (day 12) - linked list
You can move forward or backward. This function in idea is amazing!
Ansible playbook和Ansible Roles(三)
RAID disk array
The 2023 campus recruitment officially opened! Oceanbase would like to make an interview with you this spring
What is your understanding of thread priority?
Design a MySQL table for message queue to store message data
Why is pain rating important?
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)