当前位置:网站首页>Rotated Sorted Array旋转排序数组相关题
Rotated Sorted Array旋转排序数组相关题
2022-06-10 19:02:00 【bitcarmanlee】
1.什么是Rotated Sorted Array
在leetcode相关题目中,对Rotated Sorted Array相关的定义为:
整数数组 nums 按升序排列,数组中的值互不相同 。
在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
通俗地讲,旋转数组中没有相等的数值,且该数组可以被分为连续的两部分,这两部分均按升序排列。
2.寻找旋转排序数组中的最小值
leetcode中153题,就是寻找旋转数组中的最小值。
一个不包含重复元素的升序数组在经过旋转之后,可以得到下面可视化的折线图:

从图中不难看出,假设数组中最后一个元素为x,那么最小值右侧的元素一定都比这个元素小,而最小值左侧元素都比这个值大。
进行二分查找时,假设左右边界分别为low, high,中间为pivot。
- nums[pivot] < nums[high],说明nums[pivot]是最小值右侧的元素,因此忽略二分查找的右半部分。

2.nums[pivot] > nums[high],说明nums[pivot]是最小值左侧的元素,因此忽略二分查找的左半部分。
因为数组元素不重复,只要当前区间长度不为1,pivot不会与high重合。而当区间长度为1时,说明二分查找已经结束。
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];
}
注意上面代码终止时,right刚好与left相等。
3.搜索旋转排序数组
跟上面类似的题是leetcode 33题,在一个旋转排序数组中进行搜索。
解题思路与上面类似,首先找出最小值点,就可以将数据分为有序的两部分,然后看nums[0]与target的大小。如果nums[0]<target,说明要在左半部分进行二分查找。反之在右半部分进行二分查找。
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;
}
边栏推荐
- Matlab draws ellipse code according to any angle, sampling points (resolution), position and size
- Tidb - quick start, cluster setup
- 融入机器学习,让Chrome浏览器更“懂”你
- 大学生毕业季找房,VR全景看房帮你线上筛选
- 4.35V锂电充电IC
- Rmarkdown 轻松录入数学公式
- Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close registration | geek headlines
- How to add aggregation hotspots in VR panorama? How to add a content module?
- FS4060A是4.2V/3A充电IC
- Integrate machine learning to make Chrome browser more "understand" you
猜你喜欢

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年

Go language learning notes - cross domain configuration, global exception capture | web framework gin (IV)

2022.05.29 (lc_6079_price reduction)

2022.05.27 (lc_647_palindrome substring)

2022.05.26 (lc_1143_longest common subsequence)

2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory

When the college entrance examination is opened, VR panorama can see the test site in this way

2022.05.28 (lc_5_longest palindrome substring)

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

云图说|每个成功的业务系统都离不开APIG的保驾护航
随机推荐
Dock/rancher2 deploy redis:5.0.9
DDD落地实践复盘 - 记理论培训&事件风暴
Logback exclude specified package / class / method log output
First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
网上开期货账户安全吗?如何避免被骗?
在VR全景中如何添加聚合热点?内容模块如何添加?
户外太阳能野营灯移动电源方案
How to add independent hotspots in VR panoramic works?
Summary of "performance test" of special test
轻松学Pytorch-全卷积神经网络实现表情识别
仅需三步学会使用低代码ThingJS与森数据DIX数据对接
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
一文帶你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
2022最强版应届生软件测试面试攻略,助你直通大厂
Docker/Rancher2部署redis:5.0.9
Yuntu says that every successful business system cannot be separated from apig
一文详解EventMesh落地华为云的探索及实践
FPGA状态机
Rmarkdown 轻松录入数学公式
Mysql database design concept (multi table query & transaction operation)