当前位置:网站首页>【LeetCode】33. Search rotation sort array
【LeetCode】33. Search rotation sort array
2022-06-12 22:22:00 【LawsonAbs】
1. subject
In fact, this question is very difficult .
2. thought
When you get this question , My first consideration is :
- Find the rotation boundary of the array , Then we can restore the array ( Get an ordered array ), Then quickly locate according to the binary search .
But the problem is : Whether it is necessary to find the rotation boundary of the array ? In fact, there is no need . Is there any other way to solve this problem ?
In the traditional two categories , We all use mid The value at the subscript is used as the basis for judgment , But that's true in a completely ordered array , If an array is not completely ordered , Is there any other way to judge ? For example, in this question , The given condition of the title is : Arrays are originally ordered , After a rotation operation, it becomes disordered , So when you look up , We can see that in fact [left,mid-1],[mid,right] There must be an ordered array in the . for instance : In the array [6,7,1,2,3,4,5], We enumerate the number of any subscript , Will find that mid The two intervals divided into satisfy At least one interval is ordered , According to this feature , We can get a complicated version of the dichotomy . And then do analysis in these two sub intervals , The problem is solved .
3. Code
can AC The code for is as follows :
class Solution:
# obtain
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: # The first comparison here avoids the second comparison
return mid
if left == mid:
left = mid+1
continue
# At least one end is orderly
# First judge how many numbers there are , Is the left side orderly
if nums[left] <= nums[mid-1]:
if nums[left] <= target and target <=nums[mid-1]: # In the following interval, find two points
right = mid-1
else: # If it's not in this range , Maybe in the other half
left = mid+1
else : # Right order
if nums[mid] <= target and target <=nums[right]: # In the following interval, find two points
left = mid+1
else: # If it's not in this range , Is still in the left half
right = mid-1
return -1
The following code is my first impression idea ,
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]: # The first two numbers are in ascending order
else:
return idx-1 # Go straight back to 0
nxt = idx << 1 # The left one
while(idx < len(nums) and nums[idx] < nums[nxt]):
idx = nxt
# Indicates that in the interval [idx,nxt] There is a
return idx # Returns the subscript
边栏推荐
- 【LeetCode】300.最长上升子序列
- Ansible基础和常用模块(一)
- 【LeetCode】数组中第K大的元素
- 四元数简介
- Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
- 【图像去噪】基于三边滤波器实现图像去噪附matlab代码
- PE installation win10 system
- China embolic coil market trend report, technical innovation and market forecast
- Es6+ new content
- Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
猜你喜欢
随机推荐
You can move forward or backward. This function in idea is amazing!
【LeetCode】103. 二叉树的锯齿形层序遍历
打新债开户安全么,新手该怎么操作?
C # reading table data in word
NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
Lambda expression and flow optimization code
The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
微信小程序提现功能
Database daily question --- day 10: combine two tables
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
Is it safe to open an account with new bonds? How should novices operate?
Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office
3.5 测试类的setup和teardown
[sword finger offer simple] sword finger offer 24 Reverse linked list
[Jianzhi offer] Jianzhi offer 05 Replace spaces
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
【890. 查找和替换模式】
Have you really learned the common ancestor problem recently?
数据库每日一题---第10天:组合两个表









