当前位置:网站首页>【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;
}
}
边栏推荐
- Record the interesting process of using Xray for the first time
- Tee command usage example
- XA Transaction SQL Statements
- Database -- acid of transaction -- introduction / explanation
- 2837xd 代码生成——StateFlow(4)
- The primary market project galaxy will conduct public offering on coinlist on February 17
- 2837xd 代碼生成——補充(1)
- Sil/pil test of matlab code generation
- Alibaba cloud SMS service
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
猜你喜欢
What is the relationship between realizing page watermarking and mutationobserver?

2837xd 代码生成——StateFlow(1)

ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林

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

2837xd code generation module learning (4) -- idle_ task、Simulink Coder
![[unreal] animation notes of the scene](/img/97/dafde0377b7c4337e1775db64ba6a4.png)
[unreal] animation notes of the scene
![[ue5] blueprint making simple mine tutorial](/img/87/d0bec747a6b6276d63a315f88745ec.png)
[ue5] blueprint making simple mine tutorial

Skywalking theory and Practice

Memories of a chat

The primary market project galaxy will conduct public offering on coinlist on February 17
随机推荐
Mixed development of uni app -- Taking wechat applet as an example
2837xd code generation module learning (2) -- ADC, epwm module, timer0
How to handle error logic gracefully
AutoCAD - layer Linetype
Image recognition - Data Cleaning
[200 Shengxin literatures] 95 multiomics exploration TNBC
Is the C language too fat
【虚幻】武器插槽:拾取武器
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
MySQL default transaction isolation level and row lock
Cmake command - Official Document
C language: making barrels
PI control of grid connected inverter (grid connected mode)
ICLR 2022: how does AI recognize "things I haven't seen"?
PI control of three-phase grid connected inverter - off grid mode
Junit5 supports suite methods
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
2837xd code generation - Summary
The latest progress and development trend of 2022 intelligent voice technology
High level application of SQL statements in MySQL database (II)