当前位置:网站首页>【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).
边栏推荐
- 蓝桥杯单片机省赛第九届
- 蓝桥杯单片机省赛第十一届
- Oracle common SQL
- NLog use
- 傅里叶级数
- 【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
- The first practical project of software tester: web side (video tutorial + document + use case library)
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- Account management of MySQL
猜你喜欢

JVM知识点

一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起

Influence of air resistance on the trajectory of table tennis

MySQL index, transaction and storage engine

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

蓝桥杯单片机第六届温度记录器

Jetpack's livedata extension mediatorlivedata

Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?

The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
随机推荐
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
[personal notes] PHP common functions - custom functions
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
MySQL index, transaction and storage engine
初识string+简单用法(二)
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
VS2010插件NuGet
蓝桥杯单片机省赛第十一届第二场
The 8th Blue Bridge Cup single chip microcomputer provincial competition
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Imageai installation
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
Blue Bridge Cup SCM digital tube skills
Interface debugging tool simulates post upload file - apipost
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Flutter中深入了解MaterialApp,常用属性解析
Class design basis and advanced
Vite: configure IP access
蓝桥杯单片机省赛第十二届第一场