leetcode 81. 搜索旋转排序数组 II(二分查找)

已知存在一个按非降序排列的整数数组 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

解题思路

分成3钟情况讨论
7 8 9 1 2 3 4 5 6
nums[l]=1 nums[mid]=3
nums[l]<nums[mid] 所以只要tar处于nums[l]<=tar<nums[mid]中,直接在[l,mid-1]区间内查找
7 8 9 1 2 3 4 5 6
nums[l]=7 nums[mid]=3
nums[l]>nums[mid] 所以只要tar处于nums[mid]<tar<=nums[r]中,直接在[mid+1,r]区间内查找
3. 1 8 9 1 2 3 4 5 6
nums[l]=nums[mid]=1
nums[l]==nums[mid] l++ 缩小搜索范围

代码

class Solution {public boolean search(int[] nums, int target) {int l=0,r=nums.length-1;while (l<=r){int mid=(r-l)/2+l;if(nums[mid]==target) return true;if(nums[l]==nums[mid]) {l++;continue;}else if(nums[l]<nums[mid])//前半部分是有序的{if(target<nums[mid]&&target>=nums[l])r=mid-1;else l=mid+1;}else {if(target>nums[mid]&&target<=nums[r])l=mid+1;else  r=mid-1;}}return false;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391487.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用ViewContainerRef探索Angular DOM操作技术

每当我阅读中遇到&#xff0c;关于Angular中使用DOM的内容时&#xff0c;总会看到一个或几个这样的类&#xff1a;ElementRef&#xff0c;TemplateRef&#xff0c;ViewContainerRef等等。 不幸的是&#xff0c;虽然其中的一些被Angular文档或相关文章所讲述&#xff0c;但是我还…

numpy1

1、NumPy包含的内容 1、ndarrray&#xff0c;高效的多维数组&#xff0c;提供了基于数组的便捷算术操作以及灵活的广播功能&#xff1b; 2、对所有数组对象进行快速的矩阵计算&#xff0c;而无需编写循环&#xff1b; 3、提供对硬盘中的数据的读写工具&#xff0c;并对内存映射…

我如何预测10场英超联赛的确切结果

Is there a way to predict the outcome of any soccer game with 100% accuracy? The honest and simplest answer is…. no. Regardless of what your fantasy football friends say, there is absolutely no way to be 100% certain, but there is a proven, mathematical …

多迪技术总监揭秘:PHP为什么是世界上最好的语言?

PHP这么一个脚本语言&#xff0c;虽然他是web开发中&#xff0c;使用者最多的语言&#xff0c;最快最简单的语言&#xff0c;生态环境和社区积累最深厚的语言&#xff0c;作为最好的编程语言&#xff0c;多迪技术总监为你介绍&#xff1a;PHP为什么是世界上最好的语言&#xff…

aws数据库同步区别_了解如何通过使用AWS AppSync构建具有实时数据同步的应用程序

aws数据库同步区别AWS AppSync automatically updates the data in web and mobile applications in real time, and updates data for offline users as soon as they reconnect. AWS AppSync会自动实时更新Web和移动应用程序中的数据&#xff0c;并在离线用户重新连接后立即为…

leetcode 153. 寻找旋转排序数组中的最小值(二分查找)

已知一个长度为 n 的数组&#xff0c;预先按照升序排列&#xff0c;经由 1 到 n 次 旋转 后&#xff0c;得到输入数组。例如&#xff0c;原数组 nums [0,1,2,4,5,6,7] 在变化后可能得到&#xff1a; 若旋转 4 次&#xff0c;则可以得到 [4,5,6,7,0,1,2] 若旋转 4 次&#xff0…

test1

test1 转载于:https://www.cnblogs.com/Forever77/p/11434403.html

打印风车旋转效果

1 while True: 2 for i in["/","-","\\","|"]: 3 print "%s\r" %i, 转载于:https://www.cnblogs.com/feifei-cyj/p/7469333.html

深度学习数据自动编码器_如何学习数据科学编码

深度学习数据自动编码器意见 (Opinion) When I first wanted to learn programming, I coded along to a 4 hour long YouTube tutorial.刚开始学习编程时&#xff0c;我编写了长达4个小时的YouTube教程。 “Great,” I thought after finishing the course. “I know how to …

Angular 5.0 学习2:Angular 5.0 开发环境的搭建和新建第一个ng5项目

1.安装Node.js 在开始工作之前&#xff0c;我们必须设置好开发环境。如果你的机器上还没有Node.js和npm&#xff0c;请先安装它们。去Node.js的官网&#xff0c;https://nodejs.org/en/&#xff0c;点击下载按钮&#xff0c;下载最新版本&#xff0c;直接下一步下一步安装即可&…

leetcode 154. 寻找旋转排序数组中的最小值 II(二分查找)

已知一个长度为 n 的数组&#xff0c;预先按照升序排列&#xff0c;经由 1 到 n 次 旋转 后&#xff0c;得到输入数组。例如&#xff0c;原数组 nums [0,1,4,4,5,6,7] 在变化后可能得到&#xff1a; 若旋转 4 次&#xff0c;则可以得到 [4,5,6,7,0,1,4] 若旋转 7 次&#xff0…

robot:根据条件主动判定用例失败或者通过

场景&#xff1a; 当用例中的断言部分需要满足特定条件时才会执行&#xff0c;如果不满足条件时&#xff0c;可以主动判定该用例为passed状态&#xff0c;忽略下面的断言语句。 如上图场景&#xff0c;当每月1号时&#xff0c;表中才会生成上月数据&#xff0c;生成后数据不会再…

golang go语言_在7小时内学习快速简单的Go编程语言(Golang)

golang go语言The Go programming language (also called Golang) was developed by Google to improve programming productivity. It has seen explosive growth in usage in recent years. In this free course from Micheal Van Sickle, you will learn how to use Go step…

使用MUI框架,模拟手机端的下拉刷新,上拉加载操作。

套用mui官方文档的一句话&#xff1a;“开发者只需关心业务逻辑&#xff0c;实现加载更多数据即可”。真的是不错的框架。 想更多的了解这个框架&#xff1a;http://dev.dcloud.net.cn/mui/ 那么如何实现下拉刷新&#xff0c;上拉加载的功能呢&#xff1f; 首先需要一个容器&am…

图深度学习-第1部分

有关深层学习的FAU讲义 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Git上传项目到github

2019独角兽企业重金招聘Python工程师标准>>> Git入门 个人理解git就是一个上传工具&#xff0c;同时兼具和svn一样的版本控制功能&#xff08;此解释纯属本人个人观点&#xff09; Github是什么 github就是一个分布式版本管理系统&#xff08;反正我就是这么认为的…

ionic4 打包ios_学习Ionic 4并开始创建iOS / Android应用

ionic4 打包iosLearn how to use Ionic 4 in this full course for beginners from Awais Mirza. Ionic Framework is the free, open source mobile UI toolkit for developing high-quality cross-platform apps for native iOS, Android, and the web—all from a single Ja…

robot:当用例失败时执行关键字(发送短信)

使用场景&#xff1a; 当用例失败时需要通知对应人员&#xff0c;则需要在Teardown中&#xff0c;使用关键字Run Keyword If Test Failed Send Message关键字为自定义关键字&#xff0c;${content}为短信内容&#xff0c;${msg_receiver}为短信接收者列表。 当然执行成功时需要…

leetcode 263. 丑数

给你一个整数 n &#xff0c;请你判断 n 是否为 丑数 。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 丑数 就是只包含质因数 2、3 和/或 5 的正整数。 示例 1&#xff1a; 输入&#xff1a;n 6 输出&#xff1a;true 解释&#xff1a;6 2 3 …

NTP同步

RedHat Linux NTP实施步骤1、 查看本系统与NTP服务器的时间偏差 ntpdate -d 192.168.142.114 [rootzabbix-proxy ~]# ntpdate -d 192.168.142.114 24 Aug 17:26:45 ntpdate[3355]: ntpdate 4.2.6p51.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 192.168.142.…