当前位置:网站首页>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向右移动了一位.
边栏推荐
- 简单理解try catch和try finally
- PAT乙级-B1016 部分A+B(15)
- R7 6800H+RTX3050+120Hz 2.8K OLED屏,无畏Pro15 2022开启预售
- PAT乙级-B1018 锤子剪刀布(20)
- mysql占用服务器CPU100%的解决办法
- What are the visual database design software _ database visual programming
- 地球自转加快
- 2021年12月电子学会图形化一级编程题解析含答案:下雨
- FATFS | 中文显示 | 长文件名
- 测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子
猜你喜欢
随机推荐
php类的析构函数:__destruct
2021年12月电子学会图形化二级编程题解析含答案:绘制多边形
JS手写call apply bind (详细)(面试)
身为程序员的我们如何卷死别人?破局重生。
NFT盲盒挖矿DAO智能合约dapp系统开发详情
DeepLink在转转的实践
王守创:多组学整合分析揭示植物代谢多样性的分子机制(8月2号晚)
2021年12月电子学会图形化四级编程题解析含答案:聪明的小猫
新版本MaxCompute 的SQL支持 UDF 分区裁剪的逻辑是怎样的?
The general trend, another key industry related to Sino-US competition, has reached a critical moment
PAT乙级-B1010 一元多项式求导(25)
LeetCode136:只出现一次的数字
问题5:发现缺陷怎么办?缺陷的类型有哪些?
R7 6800H+RTX3050+120Hz 2.8K OLED screen, Intrepid Pro15 2022 pre-sale
PAT乙级-B1008 数组元素循环右移问题(20)
A high-performance creation book, ASUS Dreadnought Pro15 2022 is completely enough for daily photo editing and editing!
币圈提款机:Solana钱包出现未知安全漏洞 大量用户数字资产被盗
2021年12月电子学会图形化四级编程题解析含答案:新冠疫苗接种系统
兆骑科创创业大赛,双创服务平台,线上直播路演
Linux安装Mysql的几种方法








