当前位置:网站首页>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
边栏推荐
- C unit test
- uni-app跨域配置
- How to choose cloud note tool? What can I do with cloud notes?
- I came to the library applet check-in process analysis
- 关于mysql的问题,希望个位能帮一下忙
- Monitoring of debezium synchronization debezium
- Tenant issues.
- 17_ Form Data
- scipy.sparse.csr_ matrix
- ERROR: could not extract tar starting at offset 000000000000020980+9231072+2
猜你喜欢

Information System Project Manager - Chapter 10 communication management and stakeholder management examination questions over the years

第3章业务功能开发(删除线索)

图解B+树的插入过程

TCP三次握手四次挥手

Prometheus + process exporter + grafana monitor the resource usage of the process

IDEA运行web项目出现乱码问题有效解决(附详细步骤)

Ggplot2 learning summary

商业智能BI全解析,探寻BI本质与发展趋势

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

Tenant issues.
随机推荐
eslint常见报错集合
租户问题。
IDEA运行web项目出现乱码问题有效解决(附详细步骤)
如何加速矩阵乘法
1. Mx6ul core module serial -iot-6ulx core module brief introduction (I)
GAMES101复习:光栅化
17_表单数据
BigDecimal use
Ti am335x industrial control module uses the Debian system of beaglebone (BBB)
MySQL(4)
Prometheus + redis exporter + grafana monitor redis service
流形学习、、
1. Mx6ul core module serial use - touch screen calibration (IX)
Brief introduction and use of NPM link
What can EAM system help enterprises do?
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
18.删除链表的倒数第n个节点
obsidian移动端PC段同步
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
HLS实验一--乘法器