当前位置:网站首页>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).
边栏推荐
- Among the four common technologies for UAV obstacle avoidance, why does Dajiang prefer binocular vision
- 谈谈激光雷达的波长
- 2022 spring semester summary
- Redis数据持久化
- Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
- Big Five personality learning records
- SCM signal generator program
- El expression
- 四旋翼飞行器避障系统基础
- LVS four layer load balancing cluster (3) cluster function classification - HPC
猜你喜欢
随机推荐
四旋翼飞行器避障系统基础
Redis hyperloglog cardinality statistics algorithm
leetcode. 1 --- sum of two numbers
Introduction to RFM analysis
Cache read / write -- write
Sword finger offer II 022 Entry node of a link in a linked list
Example of try catch finally execution sequence
web自动化测试之webdriver api总结
Binocular vision -- creating an "optimal solution" for outdoor obstacle avoidance
谈谈激光雷达的波长
SCM: introduction to Modbus communication protocol
单片机:PCF8591 应用程序
Line height equals height why not center
leetcode.1 --- 两数之和
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
Common array methods in JS (map, filter, foreach, reduce)
手机私有充电协议解读
Hugo 博客搭建教程
EGO Planner代码解析----CMakeLists.txt和package.xml
[note]vs2015 compilation of masm32 using MASM32 Library









