当前位置:网站首页>How to use binary search and find whether the rotation in the array contains a (target) value?Rotate the sorted array leetcode 81. Search
How to use binary search and find whether the rotation in the array contains a (target) value?Rotate the sorted array leetcode 81. Search
2022-08-03 15:14:00 【Lu Yi, Twelve】
活动地址:CSDN21天学习挑战赛
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰.各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…
…
欢迎参与CSDN学习挑战赛,成为更好的自己,请参考活动中各位优质专栏博主的免费高质量专栏资源(这部分优质资源是活动限时免费开放喔~),按照自身的学习领域和学习进度学习并记录自己的学习过程.您可以从以下3个方面任选其一着手(不强制),或者按照自己的理解发布专栏学习作品,参考如下:
一.题目描述
已知存在一个按非降序排列的整数数组 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,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 [4,5,6,6,7,0,1,2,4,4] .
给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中.如果 nums 中存在这个目标值 target ,则返回 true ,否则返回 false .
你必须尽可能减少整个操作步骤.
示例 1:
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:true
示例 2:
输入:nums = [2,5,6,0,0,1,2], target = 3
输出:false
二.代码
class Solution {
public:
bool search(vector<int> &nums, int target) {
int start = 0, end = nums.size() - 1, mid = 0;
while(start <= end) {
mid = (start + end)/2;
if(nums[mid] == target) {
return 1; //true
}
//Can't tell which half is in order
if(nums[mid] == nums[start]) {
++start;
}else if(nums[mid] > nums[start]) {
//mid左侧有序,判断targetexists in the ordered sequence.
if(target >= nums[start] && target <= nums[mid]) {
end = mid - 1;
}else{
start = mid + 1;
}
}else{
//右侧有序
if(nums[mid] <= target && target <= nums[end]) {
start = mid + 1;
}else{
end = mid - 1;
}
}
}
return 0;
}
};
三.思路
1.大致方向
The dichotomy is used with increasing or decreasing sequences,Rotating arrays are not strictly incrementing or decrementing sequences,所以我们要:
1.Divide the rotated array into an ordered half and an unordered half by bisection,(例如: 对于数组 [2,3,3,3,4,4,4,5,6,7,1] 来说,第一次二分后 The ordered half is[2,3,3,3,4],The disordered half is[4,5,6,7,1]);
2.If the value you are looking for exists in the ordered half,Then you can use the regular binary process;
3.If the value you are looking for exists in the unordered half,Then repeat the process(put the current array[4,5,6,7,1]二分为[4,5]和[7,1]两部分).
2.细节
variables and their meanings:
start Left subscript of a bipartite array,初值是0;
end The right-hand subscript of the bipartite array,初值是nums.size();
mid The middle subscript of the bipartite array,值为 (start + end)/2;
@How to tell which half is in order?
@If two minutes nums[mid] > nums[start],那么mid左侧是有序的.
@If two minutes nums[mid] < nums[start],那么mid右侧是有序的.
@If two minutesnums[mid] == nums[start] ,Not sure which half is in order,此时令 start = start + 1,效果 是mid向右移动了一位.
边栏推荐
- 选择合适的 DevOps 工具,从理解 DevOps 开始
- 苹果开发「AI 建筑师」GAUDI:根据文本生成超逼真 3D 场景!
- 【软件工程之美 - 专栏笔记】36 | DevOps工程师到底要做什么事情?
- 跨桌面端之组件化实践
- What are the visual database design software _ database visual programming
- Linux安装Mysql的几种方法
- Use Typora+EasyBlogImageForTypora to write a blog and upload pictures quickly without a picture bed
- Windows服务器如何防止黑客入侵的安全设置
- 2021年12月电子学会图形化四级编程题解析含答案:聪明的小猫
- 夜神浏览器fiddler抓包
猜你喜欢
随机推荐
How to use redis
0 code 4 steps to experience IoT devices on the cloud
雷克萨斯lm的安全性如何,通过两个角度来聊这个话题
Jupyter Notebook 交互式编程 & 低代码拖拽式编程 | 数据科学生态下的理想平台
2021年12月电子学会图形化二级编程题解析含答案:消灭蝙蝠
2022-随便学学
PHP中高级面试题 – 第一天
新版本的 MaxCompute 中,SQL支持的 LIMIT OFFSET 的语法是什么功能?
问题6:下拉框测试点
HDU Largest prime factor(埃拉托色尼筛选法求素数模板法改动)
MATLAB中writetimetable函数用法
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
【问题】使用pip安装第三方库的时候遇到“timeout”的解决方法
[The Beauty of Software Engineering - Column Notes] 36 | What exactly do DevOps engineers do?
冒烟测试冒烟测试
2021年12月电子学会图形化三级编程题解析含答案:数星星
varchar2 and varchar2(char)_datetime data types
未来无法预料
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
一文搞懂$_POST和php://input的区别








