当前位置:网站首页>Binary search 33. search rotation sort array
Binary search 33. search rotation sort array
2022-07-26 02:26:00 【A little dog】
Two points search 33. Search rotation sort array
for example , [0,1,2,4,5,6,7] In subscript 3 It may turn into [4,5,6,7,0,1,2] .
Here you are. After rotation Array of nums And an integer target , If nums There is a target value in target , Returns its subscript , Otherwise return to -1 .
You have to design a time complexity of O(log n) The algorithm to solve this problem .

Start thinking
It is a more traditional binary search mode
First, you need to split an array every time , It's interesting to know , Our array needs to be divided into two cases .
- Set the intermediate value nums[mid] and nums[0] Compare the size There are two cases
- Set the intermediate value nums[mid] and nums[n-1] Compare the size There are two cases
The above division is ok
Can be effectively split
For example, because we mid Writing int mid = l + ((r-l)>>>1); Keep to the left
If you use 1. Set the intermediate value nums[mid] and nums[0] Compare the size : Need nums[0]<=nums[mid]
If you use 2. Set the intermediate value nums[mid] and nums[n-1] Compare the size : Need nums[n]<nums[mid]
Take the first case as an example

public static int search(int[] nums, int target) {
int l = 0 ,n = nums.length-1, r = n;
while(l<=r){
int mid = l + ((r-l)>>>1);
if (nums[mid]==target){
return mid;
}
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
Please correct me if there is any mistake
边栏推荐
- I came to the library applet one click sign in and one click grab location tool
- scipy.sparse.vstack
- 关于mysql的问题,希望个位能帮一下忙
- i. Mx6ull snvs power domain GPIO status hold verification
- 我来图书馆小程序签到流程分析
- GAMES101复习:光栅化
- 如何加速矩阵乘法
- 1. Mx6ul core module use serial RTC test (XII)
- Adruino basic experimental learning (I)
- Information System Project Manager - Chapter 10 communication management and stakeholder management examination questions over the years
猜你喜欢

Turn on the LED

国标GB28181协议视频平台EasyGBS消息弹框模式优化

C unit test

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"

EAM系统能帮助企业做什么?

Primary key b+ tree, secondary index b+ tree and corresponding query process analysis

Obsidian mobile PC segment synchronization

图解B+树的插入过程
![[纯理论] YOLO v4: Optimal Speed and Accuracy of Object Detection](/img/1f/f38c3b38feed9e831ad84b4bbf81c0.png)
[纯理论] YOLO v4: Optimal Speed and Accuracy of Object Detection

c# 单元测试
随机推荐
Games101 review: rasterization
prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
Recorded target detection NMS (non maximum suppression)
17_表单数据
1. Mx6ul core module serial -iot-6ulx core module brief introduction (I)
Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
[cloud native] 4.1 Devops foundation and Practice
Keil's operation before programming with C language
Prometheus + redis exporter + grafana monitor redis service
Handling process of the problem that the virtual machine's intranet communication Ping fails
17_ Form Data
Pinia plugin persist, a data persistence plug-in of Pinia
国标GB28181协议视频平台EasyGBS消息弹框模式优化
增删改查业务的快速上手
MySQL建Websites数据表
1. Mx6ul core module serial Ethernet test (VII)
Wechat applet decryption and unpacking to obtain source code tutorial
Brief introduction and use of NPM link
National standard gb28181 protocol video platform easygbs message pop-up mode optimization