当前位置:网站首页>寻找旋转排序数组中的最小值 II[经典抽象二分 + 如何破局左中右三者相等]
寻找旋转排序数组中的最小值 II[经典抽象二分 + 如何破局左中右三者相等]
2022-06-27 00:08:00 【REN_林森】
经典抽象二分+如何破局左中右三者相等
前言
这个题是我做过非常经典的抽象二分,不仅判定规则被抽象,而且还会出现判定规则“失灵”现象。
如何破局?且需挖掘给定数组的特征,利用个性,顺势而为(正向/逆向思维),破掉难题,这便是逻辑,有因必有果,利用其因必得其果,不是偶然。
一、寻找旋转排序数组中的最小值 II

二、经典抽象二分
package everyday.medium;
public class FindMin {
/* target:寻找最小值,直接抽象二分,但是注意前 中 尾三者相等的情况。 将旋转和未旋转的子数组分为两个子数组看。 如何抽象?让nums[mid] 和 nums[0] 比较,若大于nums[0] 则mid必在第一个子数组;如果小于nums[0],那mid一定在第二个子数组中。 特殊情况:当nums[mid] = nums[0]时,则看nums[mid] 和 nums[high]的关系,若 不等则必是nums[mid] > nums[high],那mid一定在第一个子数组中。 那若是nums[mid] 也等于 nums[high]呐?如下, [2,2,2,2,2,2,2,0,1,2] | [2,2,2,0,1,2,2,2,2,2,2] 如何破局?注意这两个子数组的个性问题,都是递增的;最大值和最小值挨着,也是唯一一对数,它俩不是递增。 当左中右相等时,则不急着取mid,可low往前靠一步,破掉这种三者相等的情况。 若low位就是最大位怎么办?这时可不能low++! 看nums[low] 是否大于nums[low + 1],若是,nums[low]必是数组的最大值,最小值就紧跟其后,直接返回。 为什么不high--来破局?因为如果high已经在最大位上,high是不能动的。为什么不能像low 和 low + 1来判断,因为两个子数组都是递增。唯一一对非递增还是从前面过来才能碰到的。 */
public int findMin(int[] nums) {
// 二分寻最大值,最小值会紧跟其后,抽象二分。
int low = 0, high = nums.length - 1;
// 二分快速找到比nums[0]小的,第一个小的。
int first = nums[0];
while (low < high) {
int mid = low + (high - low + 1 >>> 1);//这里+1很重要,需要去上整,配合下面的low = mid防止死循环。
int midVal = nums[mid];
if (midVal > first) low = mid;
else if (midVal < first) high = mid - 1;
// 破局的关键代码。
else if (midVal == nums[high]) {
// 三个重要案例
// [3,3,1,3] [3,1,3,3] [2,2,2,0,2,2]
if (nums[low] <= nums[low + 1]) low++;
// 最大值和最小值挨着,也是唯一一对数,它俩不是递增。
else return nums[low + 1];
}
// 可合并。
else low = mid;
}
return high + 1 == nums.length ? nums[0] : nums[high + 1];
}
}
总结
1)经典抽象二分练习。
2)利用其因(题目的显示 | 隐式(需细心挖掘) 条件/个性特征),必得其果,不会偶然,这便是逻辑。
参考文献
边栏推荐
- How to open an account on the mobile phone? Is it safe to open an account online and speculate in stocks
- xml学习笔记
- Lambda表达式
- [microservices] understanding microservices
- Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
- An article takes you to learn container escape
- Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
- 大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
- 泰国安全又划算的支付方式
- 消息队列简介
猜你喜欢

My advanced learning notes of C language ----- keywords

气液滑环与其他滑环的工作原理有什么区别

How to easily describe the process of machine learning?

复杂数据没头绪?

05 | standard design (Part 2): how to standardize the different styles of commit information, which are difficult to read?

如何通俗易懂的描述机器学习的流程?

目标追踪拍摄?目标遮挡拍摄?拥有19亿安装量的花瓣app,究竟有什么别出心裁的功能如此吸引用户?

com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x

Special topic II on mathematical physics of the sprint strong foundation program

An article takes you to learn container escape
随机推荐
In the Internet industry, there are many certificates with high gold content. How many do you have?
【UVM实战 ===> Episode_3 】~ Assertion、Sequence、Property
大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
串口调试工具 mobaxterm 下载
Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
大咖讲 | 最前沿的昇思MindSpore开源社区运营的经验分享,快拿出小本本记录呀!
MATLAB data type - character type
能在手机上开户炒股吗 网上开户炒股安全吗
[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories
如何通俗易懂的描述机器学习的流程?
Great health industry annual must attend event, 2022 Shandong International Great Health Industry Expo
The fourth bullet of redis interview eight part essay (end)
Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!
Is it reliable to speculate in stocks by mobile phone? Is it safe to open an account and speculate in stocks online
The most complete hybrid precision training principle in the whole network
1+1<2 ?! Interpretation of hesic papers
Super hard core! Can the family photo album on Huawei's smart screen be classified automatically and accurately?
Freescale 单片机概述
com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x
简单快速的数网络(网络中的网络套娃)