当前位置:网站首页>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 !
边栏推荐
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- [Androd] Gradle 使用技巧之模块依赖替换
- LDC Build Shared Library
- 异步、邮件、定时三大任务
- leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
- MySQL基础用法02
- 【FH-GFSK】FH-GFSK信号分析与盲解调研究
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- MySQL foundation 05 DML language
- 机器学习术语
猜你喜欢

Cut point of undirected graph

Leetcode 6103 - minimum fraction to delete an edge from the tree

Detailed explanation of Q-learning examples of reinforcement learning

Basis of information entropy

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】

excel IF公式判断两列是否相同

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

excel表格计算时间日期的差值,并转化为分钟数
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

【无标题】
随机推荐
Every k nodes in the linked list are flipped
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
关于Fibonacci数列
Database SQL language 01 where condition
Daily topic: movement of haystack
[Arduino experiment 17 L298N motor drive module]
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
465. DFS backtracking of optimal bill balance
Machine learning terminology
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
The meaning of wildcard, patsubst and notdir in makefile
Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
按键精灵打怪学习-自动寻路回打怪点
Do not log in or log in to solve the problem that the Oracle database account is locked.
每日一题之干草堆的移动
tp6快速安装使用MongoDB实现增删改查
ThinkPHP+Redis实现简单抽奖
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
Create your first Kivy program Hello word (tutorial includes source code)