当前位置:网站首页>Find the minimum value of flipped array [Abstract bisection]
Find the minimum value of flipped array [Abstract bisection]
2022-06-25 23:56:00 【REN_ Linsen】
Abstract dichotomy exercise
Preface
Abstract dichotomy , Common test questions , With the idea of binary fast approximation , Abstract judgment rules , Realize low time complexity search target.
One 、 Find the minimum value of the flipped array

Two 、 Abstract dichotomy + Equal treatment details of the first, middle and last three
package everyday.medium;
public class FindMin {
/* target: Find the minimum , Direct Abstract dichotomy , But notice before in When the last three are equal . [2,2,2,2,2,2,2,0,1,2] | [2,2,2,0,1,2,2,2,2,2,2] broken ? When left, middle and right are equal , Then there is no hurry to take mid, Instead, go one space to the left on the right , So the value on the left is less than or equal to nums[high] Of . Finally get the tail of the first array i, be i+1 Even if the minimum value is . */
public int findMin(int[] nums) {
// Bisection search k, Abstract dichotomy .
int low = 0, high = nums.length - 1;
// Two points can be found quickly nums[0] Small , The first small one .
int first = nums[0];
while (low < high) {
int mid = low + (high - low + 1 >>> 1);// here +1 Very important , It needs to be rectified , With the following low = mid
int midVal = nums[mid];
if (midVal > first) low = mid;
else if (midVal < first) high = mid - 1;
else if (midVal == nums[high]) high--;// Take a step to the left , Walking edge is a smaller value
// Can merge .
else low = mid;
}
return low + 1 == nums.length ? nums[0] : nums[low + 1];
}
}
summary
1) Abstract dichotomy exercise , Rounding details of dichotomy + low/high No carry details .
reference
边栏推荐
猜你喜欢

(转载)进程和线程的形象解释

Unsigned and signed vernacular

C# IO Stream 流(二)扩展类_封装器

Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog

利用VBScript连接mysql数据库_过路老熊_新浪博客

达梦数据库修改字段信息采坑记

兆欧表电压档位选择_过路老熊_新浪博客

Lazy people teach you to use kiwi fruit to lose 16 kg in a month_ Old bear passing by_ Sina blog

7.常用指令(下)v-on,v-bind,v-model的常见操作

js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
随机推荐
猕猴桃酵素的功效_过路老熊_新浪博客
Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
Compiling protobuf protocol files using makefile in PHP
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
DPVS-FullNAT模式keepalived篇
在step7中实现模拟量数值与工程量数值之间的转换_过路老熊_新浪博客
SSM整合学习笔记(主要是思路)
在win10下使用visual studio2015链接mysql数据库
STEP7主站与远程I/O组网_过路老熊_新浪博客
关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
JS中的原型链面试题--Foo和 getName
Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
动态验证码
详细讲解局部变量、全局变量、静态变量三种类型
Jenkins 发布PHP项目代码
Oracle writes a trigger that inserts a piece of data first and updates a field in the data
php中使用Makefile编译protobuf协议文件
Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
用ES5的方式实现const
Talk about singleton mode!