当前位置:网站首页>Leetcode 33. Search rotation sort array
Leetcode 33. Search rotation sort array
2022-06-27 16:58:00 【chenyson】
difficulty : secondary
frequency :124
** subject :** An array of integers nums In ascending order , The values in the array Different from each other .
Before passing it to a function ,nums In some unknown subscript k(0 <= k < nums.length) On the rotate , Make array [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]]( Subscript from 0 Start Count ). 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 .
Their thinking : Orderly ? Partial order ?==> Dichotomy search
- See which parts are ordered and target In this section , This part uses the binary search method
- details stay int In the array ,length Attribute , And in the String in ,length Is the method
- It is divided into the left side , Order on the right ;
- The order on the left is divided into The value is on the left , The value is not on the left
- The order on the right is divided into Value on right , The value is no longer on the right
class Solution {
public int search(int[] nums, int target) {
int len=nums.length;
if(len==0) return -1;
if(len==1) {
if(target==nums[0]){
return 0;
}
}
int l=0;
int r=len-1;
// Because the parts are orderly , To the end , There will always be order
// Split the array in two , One of them must be orderly , Another possibility is order , It can also be partially ordered .
// At this time, the ordered part is searched by dichotomy . The disordered part is divided into two parts , One of them must be orderly , Another may be orderly , Possible disorder .
while(l<=r){
int mid=(l+r)/2;
if(nums[mid]==target){
return mid;
}
// Order on the left
if(nums[l]<=nums[mid]){
// The left is ordered and the value is on the left
if(nums[l]<=target &&target<nums[mid]){
r=mid-1;
}
// Order on the left but Value on right
else{
l=mid+1;
}
}
// Order on the right
else{
// The right is ordered and the value is on the right
if(nums[mid]<target&&target<=nums[r]){
l=mid+1;
}
// Order on the right But the value is on the left
else{
r=mid-1;
}
}
}
return -1;
}
}
边栏推荐
- Extract field year / quarter effect based on date
- 模拟进程调度
- Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
- 2/14 preliminary calculation geometry
- 深耕数字化,引领云原生,服务更多开发者
- Annual comprehensive analysis of China's audio market in 2022
- Autodesk NavisWorks 2022 software installation package download and installation tutorial
- [pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
- d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
- C语言教师工作量管理系统
猜你喜欢
![[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth](/img/70/fa79ba38e28c41ed28bce2ec73cd79.png)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Sliding window + monotone queue concept and example (p1886 Logu)
![[pyGame games] this](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)

鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態

Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
![[fxcg] today's market analysis](/img/97/fc6faa5ab693e383f085b407ce1d85.jpg)
[fxcg] today's market analysis

树莓派初步使用

米哈游起诉五矿信托,后者曾被曝产品暴雷

LeetCode 124. Binary tree maximum path sum - binary tree series question 8
随机推荐
10 minutes to master the installation steps of MySQL
#yyds干货盘点#简述chromeV8引擎垃圾回收
Missing d3d10 How to repair DLL files? Where can I download d3d10.dll
Yyds dry inventory brief chrome V8 engine garbage collection
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
Extract field year / quarter effect based on date
Detailed explanation of transaction isolation level
Practice of constructing ten billion relationship knowledge map based on Nebula graph
Event listening mechanism
等保2.0密码要求是什么?法律依据有哪些?
Kubernetes basic self-study series | introduction to ingress API
Oracle concept II
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
Kubernetes基础自学系列 | Ingress API讲解
The array of C language is a parameter to pass a pointer
LACP details
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
C système de gestion de la charge de travail des enseignants en langues
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth