当前位置:网站首页>【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).
边栏推荐
- 毕设-基于SSM电影院购票系统
- Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
- A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
- 【IBDFE】基于IBDFE的频域均衡matlab仿真
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝桥杯单片机省赛第七届
- Network connection mode of QT
- [mv-3d] - multi view 3D target detection network
- VS2010插件NuGet
- Lost a few hairs, and finally learned - graph traversal -dfs and BFS
猜你喜欢

The 7th Blue Bridge Cup single chip microcomputer provincial competition

High performance and low power cortex-a53 core board | i.mx8m Mini

A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)

Influence of air resistance on the trajectory of table tennis
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
![[designmode] Prototype Pattern](/img/ee/c4e48c2ce8ff66f50f0bf13e5a0418.png)
[designmode] Prototype Pattern

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走

Interface debugging tool simulates post upload file - apipost

Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface

【DesignMode】原型模式(prototype pattern)
随机推荐
蓝桥杯单片机省赛第五届
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Where can I buy cancer insurance? Which product is better?
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
BiShe cinema ticket purchasing system based on SSM
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Class design basis and advanced
MySQL index, transaction and storage engine
The fourth provincial competition of Bluebridge cup single chip microcomputer
High performance and low power cortex-a53 core board | i.mx8m Mini
NLog使用
Which product of anti-cancer insurance is better?
Xlwings drawing
Wpviewpdf Delphi and Net PDF viewing component
Kotlin basic learning 15
C语言:逻辑运算和判断选择结构例题
Object oriented thinking
go 函数
Jetpack's livedata extension mediatorlivedata