当前位置:网站首页>Sword finger offer 11 Minimum number of rotation array - binary lookup
Sword finger offer 11 Minimum number of rotation array - binary lookup
2022-06-13 04:11:00 【hequnwang10】
One 、 Title Description
Move the first elements of an array to the end of the array , We call it rotation of arrays .
Give you a chance to exist repeat An array of element values numbers , It turns out to be an ascending array , And a rotation is carried out according to the above situation . Please return the smallest element of the rotation array . for example , Array [3,4,5,1,2] by [1,2,3,4,5] A rotation of , The minimum value of the array is 1.
Be careful , Array [a[0], a[1], a[2], …, a[n-1]] Rotate once The result is an array [a[n-1], a[0], a[1], a[2], …, a[n-2]] .
Example 1:
Input :numbers = [3,4,5,1,2]
Output :1
Example 2:
Input :numbers = [2,2,2,0,1]
Output :0
Two 、 Problem solving
Two points search
This question has duplicate numbers , There are three situations
The median == Right value :right -= 1;
The median < Right value : right = mid;
The median > Right value :left = mid + 1;
class Solution {
public int minArray(int[] numbers) {
// This question contains repeated numbers , So there are three cases
int left = 0,right = numbers.length - 1;
while(left < right){
int mid = left+(right - left) / 2;
if(numbers[mid] < numbers[right]){
right = mid;
}else if(numbers[mid] > numbers[right]){
left = mid + 1;
}else{
right -= 1;
}
}
return numbers[left];
}
}
Time complexity :O(log(n);
Spatial complexity :O(1).
边栏推荐
- Value of line height
- MCU: EEPROM multi byte read / write operation sequence
- [test development] automated test selenium (II) -- common APIs for webdriver
- Use ASE encryption and decryption cache encapsulation in Vue project
- EGO planner论文翻译
- Koa file upload and download
- Promise combined with await
- 【自动化测试】关于unittest你需要知道的事
- 谈谈激光雷达的波长
- 高等数学(第七版)同济大学 习题1-3 个人解答
猜你喜欢

USB-IF BC1.2充电协议解读

单片机:A/D 和 D/A 的基本概念

环评图件制作-数据处理+图件制作

Goframe day 4

1.4.2 Capital Market Theroy

大疆无人机飞控系统的原理、组成及各传感器的作用

出现Could not find com.scwang.smart:refresh-layout-kernel:2.0.3.Required by: project :app 无法加载第三方包情况

EGO planner论文翻译

单片机:Modbus 多机通信程序设计

Real time requirements for 5g China Unicom repeater network management protocol
随机推荐
扫地机器人如何才能避障不“智障”?五种主流的避障技术解析
EIA map making - data processing + map making
Ego planner code analysis ----cmakelists Txt and package xml
Modeling discussion series 143 data processing, analysis and decision system development
Difference between OKR and KPI
On the value of line height
ROS topics and nodes
Introduction and use of ES6
MCU: pcf8591 hardware interface
Single chip microcomputer: basic concepts of a/d and d/a
单片机:A/D 和 D/A 的基本概念
史上最详细的Swin-Transformer 掩码机制(mask of window attentation)————shaoshuai
The WebView case of flutter
Precautions for stream flow
大疆无人机飞控系统的原理、组成及各传感器的作用
【ZeloEngine】本地化流程/ImGui中文化
[web] cookies and sessions
MCU: EEPROM multi byte read / write operation sequence
Lambda end operation find and match findany
Promise combined with await