当前位置:网站首页>【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).
边栏推荐
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- Basic syntax of unity script (6) - specific folder
- Unity脚本的基础语法(7)-成员变量和实例化
- Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- Raspberry pie GPIO pin controls traffic light and buzzer
- ImageAI安装
- 【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
- Qt的网络连接方式
- Monkey测试
猜你喜欢
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
[yolo3d]: real time detection of end-to-end 3D point cloud input
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
Didi open source Delta: AI developers can easily train natural language models
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
[designmode] builder model
蓝桥杯单片机第六届温度记录器
蓝桥杯单片机省赛第八届
随机推荐
Oracle common SQL
C language: examples of logical operation and judgment selection structure
The first practical project of software tester: web side (video tutorial + document + use case library)
蓝桥杯单片机数码管技巧
[personal notes] PHP common functions - custom functions
go 语言命名规范
[designmode] builder model
Oracle的md5
Vite: configure IP access
数据库文件逻辑结构形式指的是什么
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
【DesignMode】原型模式(prototype pattern)
Pycharm2021 delete the package warehouse list you added
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Learn more about materialapp and common attribute parsing in fluent
Vite: scaffold assembly
The 9th Blue Bridge Cup single chip microcomputer provincial competition
蓝桥杯单片机省赛第十二届第二场
Kotlin basic learning 15
潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)