当前位置:网站首页>Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
2022-07-03 01:24:00 【Small skeleton】

Post the code first :
class Solution {
public int minArray(int[] numbers) {
int left = 0; // Define the left pointer
int right = numbers.length-1; // Define the right pointer
if (right == 0){
return numbers[0];
}
while (left < right){
int mid = left + (right - left) / 2; // Define intermediate pointer
if (numbers[mid] > numbers[right]){ // If the middle value is greater than the rightmost value , It indicates that the dividing point is mid On the right
left = mid + 1;
}else if (numbers[mid] < numbers[right]){ // If the middle value is less than the rightmost , Then the dividing point is mid On the left
right = mid;
}else if (numbers[mid] == numbers[right]){ // If the middle value is equal to the left and right , Then the violence narrows
right--;
}
}
return numbers[left];
}
}Their thinking :
The title gives us a rotated , And there may be an array of duplicate elements numbers , It is required to find the minimum value of this array .
If you don't consider the particularity of this array , Just find the minimum value in the array , I believe everyone knows to traverse the array first , Compare the previous element with the latter , Until the minimum value is found after traversing the array . Or sort the array first , Directly output the first element of the array .
These are some elementary ways of writing , If the questions are these , Then you won't get the array with repeated elements after the rotation at all . So if you want to solve problems efficiently , We must start with this special array . So let's take a look at , What is passing once “ rotate ” Array after .
“ Move the elements at the beginning of an array to the end of the array ”, This practice is called array “ rotate ”. For example, an array of 【1,2,3,4,5】 After rotation, it becomes 【4,5,1,2,3】:

From the question , Before the array starts to rotate, it is an ascending array , Then we can come to the conclusion that , An array is rotated once , It is composed of two ascending arrays . in other words , This new array , It is composed of two ascending arrays .
So at this point , You can use binary search , To find the minimum value of this new array . Now there is a question , Isn't binary search applicable to an ordered array ? These two ordered arrays are combined , Does it apply ?
Let's see , Whether dichotomy is suitable for the situation just now . It can be seen from the above figure , The rotated array is moved from the front elements to the back , So the smallest element must be the first element of the next ordered array , That is, where the next ordered array and the previous ordered array want to contact , It is called the dividing point ! So corresponding , Once we find the dividing point , The minimum value is found . So how to find the dividing point ?
Let's first set the left and right boundaries of the array left and right , And find the middle position , Set to mid , Then there is :

When mid Less than right when , Explain what ? Description from mid Start Back until right , Is an ordered array in ascending order , Then the dividing point may be mid On the right ? impossible ! So I want to find the dividing point , Only in mid To the right . Then the above array becomes :

Now let's judge , When mid dayu right when , What does this mean ? Description from mid Start To right , There is a fault in the middle , Then this fault is the dividing point we are looking for ! So let the left boundary left Turn into mid + 1 .

When left and right When both point to the same value , It means that the dividing point has been found , Then it means that the minimum value of the array is found . At this time, let's go back and analyze , We just solved the problem , Is the method of finding the dividing point the binary search method ?
But in the above process, we have neglected a problem , The title indicates that there may be duplicate elements in the array , So when repeated elements appear , Does the above analysis still work ? Obviously, it doesn't work anymore ! Because when mid and right and left When the values of are equal , It is impossible to judge whether it is mid Is the left side an ordered array or mid On the right It's an ordered array . What should I do at this time ? As shown in the figure below :

Now? mid、left、right It's all the same value , It's impossible to judge who is orderly , Now we can only narrow the scope manually , take right Move one bit to the right , namely right -- ! such , It becomes :

At this time, we can judge !
边栏推荐
- leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
- 按鍵精靈打怪學習-多線程後臺坐標識別
- Key wizard play strange learning - front desk and Intranet send background verification code
- R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
- Specified interval inversion in the linked list
- 软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
- Thinkphp+redis realizes simple lottery
- MySQL基础用法02
- MySQL foundation 04 MySQL architecture
- Leetcode 2097 - Legal rearrangement of pairs
猜你喜欢

leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】

Draw love with go+ to express love to her beloved

Assets, vulnerabilities, threats and events of the four elements of safe operation

软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053

Basic remote connection tool xshell

【FPGA教程案例6】基于vivado核的双口RAM设计与实现

Basic concept and implementation of overcoming hash
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

Correctly distinguish the similarities and differences among API, rest API, restful API and web service
随机推荐
[Androd] Gradle 使用技巧之模块依赖替换
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
合并K个已排序的链表
Top ten regular spot trading platforms 2022
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
Draw love with go+ to express love to her beloved
d,ldc構建共享庫
无向图的割点
攻克哈希的基本概念与实现
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
dotConnect for PostgreSQL数据提供程序
Leetcode 2097 - Legal rearrangement of pairs
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
按键精灵打怪学习-多线程后台坐标识别
Matlab Doppler effect produces vibration signal and processing
Linear programming of mathematical modeling (including Matlab code)
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
Button wizard play strange learning - go back to the city to buy medicine and add blood
d,ldc构建共享库