当前位置:网站首页>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;
}
}
边栏推荐
- 当发布/订阅模式遇上.NET
- Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
- Mode setting of pulseaudio (21)
- 2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
- 06. First introduction to express
- Autodesk Navisworks 2022软件安装包下载及安装教程
- 如何提升IT电子设备效能管理
- d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
- Impressive questions
猜你喜欢

Related configuration commands of Huawei LACP

Handling method of occasional error reporting on overseas equipment

Practice of constructing ten billion relationship knowledge map based on Nebula graph

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

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

EMQ 助力青岛研博建设智慧水务平台

Kubernetes basic self-study series | introduction to ingress API

Julia constructs diagonal matrix

IDE Eval reset unlimited trial reset

d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
随机推荐
IDE Eval reset unlimited trial reset
The array of C language is a parameter to pass a pointer
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
C language teacher workload management system
EMQ 助力青岛研博建设智慧水务平台
阿里云刘珅孜:云游戏带来的启发——端上创新
Special function calculator
QT audio playback upgrade (7)
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
事件监听机制
Oracle concept 3
C système de gestion de la charge de travail des enseignants en langues
How to improve it electronic equipment performance management
Practice of constructing ten billion relationship knowledge map based on Nebula graph
Mode setting of pulseaudio (21)
P. Simple application of a.r.a method in Siyuan (friendly testing)
LACP details
Yyds dry inventory brief chrome V8 engine garbage collection