当前位置:网站首页>【leetcode】81. Search rotation sort array II
【leetcode】81. Search rotation sort array II
2022-07-02 03:52:00 【Chinese fir sauce_】
subject :
81. Search rotation sort array II
It is known that there is an array of integers in non descending order nums , The values in the array don't have to be 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,4,4,5,6,6,7] In subscript 5 It may turn into [4,5,6,6,7,0,1,2,4,4] .
Here you are. After rotation Array of nums And an integer target , Please write a function to determine whether the given target value exists in the array . If nums There is a target value in target , Then return to true , Otherwise return to false .
You must minimize the whole procedure .
Example 1:
Input :nums = [2,5,6,0,0,1,2], target = 0
Output :true
Example 2:
Input :nums = [2,5,6,0,0,1,2], target = 3
Output :false
Tips :
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
Topic data assurance nums Rotated on a previously unknown subscript
-104 <= target <= 104
Dichotomy :
class Solution {
public boolean search(int[] nums, int target) {
int n = nums.length;
int l = 0;
int r = n - 1;
while(l <= r){
int mid = (l + r) / 2;
if(nums[mid] == target) return true;
// Order on the right
else if(nums[mid] < nums[r]){
if(target > nums[mid] && target <= nums[r]){
l = mid + 1;
}
else{
r = mid - 1;
}
}
// Order on the left
else if(nums[mid] > nums[r]){
if(target < nums[mid] && target >= nums[l]) {
r = mid - 1;
}
else{
l = mid + 1;
}
}
// Right side weight removal
else r--;
}
return false;
}
}
- Time complexity :O(n), among n It's an array nums The length of . In the worst case, the array elements are equal and not target, We need to visit all locations to get results .
- Spatial complexity :O(1)O(1).
边栏推荐
- 一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
- 数据库文件逻辑结构形式指的是什么
- Blue Bridge Cup SCM digital tube skills
- [untitled] basic operation of raspberry pie (2)
- NLog use
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
- Which product of anti-cancer insurance is better?
- JS generate random numbers
- C language: examples of logical operation and judgment selection structure
- Class design basis and advanced
猜你喜欢
随机推荐
Kotlin basic learning 14
Which product of anti-cancer insurance is better?
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
蓝桥杯单片机省赛第十一届第二场
JS generate random numbers
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
UI (New ui:: MainWindow) troubleshooting
Failed to upgrade schema, error: “file does not exist
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
go 包的使用
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
数据库文件逻辑结构形式指的是什么
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
傅里叶级数
Fourier series








