当前位置:网站首页>Rotated sorted array
Rotated sorted array
2022-06-10 20:07:00 【bitcarmanlee】
1. What is? Rotated Sorted Array
stay leetcode In related topics , Yes Rotated Sorted Array The relevant definition is :
An array of integers nums In ascending order , The values in the array are different from each other .
Before passing it to a function ,nums In some unknown subscript k(0 <= k < nums.length) On the rotate , Make array [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]]( Subscript from 0 Start Count ). for example , [0,1,2,4,5,6,7] In subscript 3 It may turn into [4,5,6,7,0,1,2] .
informally , There are no equal values in the rotation array , And the array can be divided into two consecutive parts , The two parts are arranged in ascending order .
2. Look for the minimum value in the rotation sort array
leetcode in 153 topic , Is to find the minimum value in the rotation array .
An ascending array that does not contain repeating elements is rotated , You can get the following visual line chart :

It's not hard to see from the picture , Suppose the last element in the array is x, Then the elements to the right of the minimum must be smaller than this element , The left element of the minimum value is larger than this value .
When performing binary search , Suppose the left and right boundaries are low, high, In the middle pivot.
- nums[pivot] < nums[high], explain nums[pivot] Is the element to the right of the minimum , So ignore the right half of the binary search .

2.nums[pivot] > nums[high], explain nums[pivot] Is the element to the left of the minimum , So ignore the left half of the binary search .
Because the array elements are not repeated , As long as the current interval length is not 1,pivot Not with high coincidence . And when the interval length is 1 when , This indicates that the binary search has ended .
int run() {
int nums[] = {4,5,6,7,0,1,2};
int n = sizeof(nums)/ sizeof(nums[0]);
int left = 0, right = n - 1;
while(left < right) {
int pivot = (left + right) / 2;
if (nums[pivot] < nums[right]) {
right = pivot;
} else {
left = pivot + 1;
}
}
return nums[left];
}
Note that when the above code terminates ,right It happens to be with left equal .
3. Search rotation sort array
A similar question is leetcode 33 topic , Search in a rotationally sorted array .
The solution is similar to the above , First, find the minimum point , You can divide the data into two ordered parts , And then look nums[0] And target Size . If nums[0]<target, Explain that you want to perform a binary search in the left half . On the contrary, binary search is performed in the right half .
int run2() {
int nums[] = {4,5,6,7,0,1,2};
int n = sizeof(nums)/sizeof(nums[0])-1;
int left = 0, right = n-1, target = 0;
while(left < right) {
int pivot = (left + right) / 2;
if (nums[pivot] < nums[right]) {
right = pivot;
} else {
left = pivot + 1;
}
}
int curleft = 0, curright = 0;
if (nums[0] < target) {
curleft = 0, curright = left - 1;
} else {
curleft = left, curright = n-1;
}
while(curleft <= curright) {
int mid = (curleft + curright) / 2;
if (nums[mid] < target) {
curleft = mid + 1;
} else if (nums[mid] > target) {
curright = mid - 1;
} else {
return mid;
}
}
return -1;
}
边栏推荐
- Nature Biotechnol | 李家洋/余泓团队利用平铺删除策略打破性状连锁,突破水稻产量瓶颈
- One article explains in detail the exploration and practice of eventmesh landing on Huawei cloud
- Routine solution - the problem of horse walking on the chessboard
- Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
- First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
- 腾讯Libco协程开源库 源码分析(二)---- 柿子先从软的捏 入手示例代码 正式开始探究源码
- 站在今天这样一个时间节点上,或许对产业互联网有一个更加明晰的认识
- One question to explain the past and present life of dynamic planning
- DDD落地实践复盘 - 记理论培训&事件风暴
- 补水仪108K加湿器开发方案_单片机_NY8A051F_单片机开发设计开发
猜你喜欢

Summary of "performance test" of special test

Flutter series: UI layout introduction

The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?

2022.05.28 (lc_5_longest palindrome substring)

Rotated Sorted Array旋转排序数组相关题

刷脸认证如何实现人脸又快又准完成校验?

Source code analysis of Tencent libco collaboration open source library (III) -- Exploring collaboration switching process assembly register saving and efficient collaboration environment

基于改进SEIR模型分析上海疫情

How to query the database table storage corresponding to a field on the sapgui screen

2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
随机推荐
Logback排除指定包/类/方法日志输出
详细解读TPH-YOLOv5 | 让目标检测任务中的小目标无处遁形
OFFICE技术讲座:标点符号-中文-大全
Is it safe to open a futures account online? How to avoid being cheated?
SBC芯片35584数据手册预调节器翻译
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
Does the giraffe's neck grow longer not because it eats leaves from high places? Scientists have found the answer in fossils 17million years ago
Analyse du code source de Tencent libco CO CO - Process open source library
首批!青藤通过信通院CWPP能力评估检验
2022.05.27 (lc_647_palindrome substring)
Summary of "performance test" of special test
如何使用物联网低代码平台进行工作表管理?
历时2年442位作者132个机构!Google发布语言模型评价新基准BIG-bench,204个任务全面评价语言模型能力,附论文
I drew several exquisite charts with plotly, which turned out to be beautiful!!
Source code analysis of Tencent libco collaborative process open source library (II) -- persimmon starts from the soft pinch, and the sample code officially begins to explore the source code
How to apply VR panorama in home decoration? Experience the real home decoration effect
2022.05.28 (lc_516_longest palindrome subsequence)
大厂是怎么写数据分析报告的?
Is it safe to open an account online for futures? How to open an account specifically
frp reverse proxy