当前位置:网站首页>【leetcode】33. Search rotation sort array
【leetcode】33. Search rotation sort array
2022-07-02 10:13:00 【Chinese fir sauce_】
subject :
33. Search rotation sort array
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 .
Example 1:
Input :nums = [4,5,6,7,0,1,2], target = 0
Output :4
Example 2:
Input :nums = [4,5,6,7,0,1,2], target = 3
Output :-1
Example 3:
Input :nums = [1], target = 0
Output :-1
Tips :
1 <= nums.length <= 5000
-10^4 <= nums[i] <= 10^4
nums Each value in the unique
Topic data assurance nums Rotated on a previously unknown subscript
-10^4 <= target <= 10^4
Two points search :
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target){
return mid;
}
// The right half is in order ( Use the first and last arrays in the ordered half segment to judge whether the target value is within this region )
else if (nums[r] > nums[mid]) {
if(nums[mid] < target && nums[r] >= target ){
l = mid + 1;
}
else {
r = mid - 1;
}
}
// The left half is in order ( Use the first and last arrays in the ordered half segment to judge whether the target value is within this region )
else {
if(nums[mid] > target && nums[l] <= target){
r = mid - 1;
}
else {
l = mid + 1;
}
}
}
return -1;
}
}
边栏推荐
- Project practice, redis cluster technology learning (VII)
- Judging right triangle in C language
- What wires are suitable for wiring on bread board?
- 【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
- 【UE5】蓝图制作简单地雷教程
- 2837xd 代碼生成——StateFlow(4)
- Blender multi lens (multi stand) switching
- 2837xd code generation module learning (2) -- ADC, epwm module, timer0
- Introduction to go language
- QT qlabel style settings
猜你喜欢
What is the relationship between realizing page watermarking and mutationobserver?

Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels

2837xd代码生成模块学习(1)——GPIO模块

2837xd code generation - Supplement (3)

虚幻材质编辑器基础——如何连接一个最基本的材质

ESLint 报错

2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules

2837xd code generation - stateflow (4)

UE4 night lighting notes

Junit4运行mvn test 测试套件升级方案
随机推荐
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
Judging right triangle in C language
The road is blocked and long, and the line is coming
QT signal slot summary -connect function incorrect usage
Project practice, redis cluster technology learning (IX)
Junit5 supports suite methods
2837xd code generation - Summary
Kinect DK obtains color RGB images in cv:: mat format (used in openpose)
UE5——AI追逐(蓝图、行为树)
Binary and decimal system of C language
Vs+qt set application icon
2837xd代码生成模块学习(4)——idle_task、Simulink Coder
[unreal] animation notes of the scene
Translation d30 (with AC code POJ 28:sum number)
How to achieve the top progress bar effect in background management projects
[ue5] animation redirection: how to import magic tower characters into the game
职业规划和发展
Bugkuctf-web24 (problem solving ideas and steps)
Matlab生成dsp程序——官方例程学习(6)
渗透测试的介绍和防范