【Leetcode】33. Search in Rotated Sorted Array
Question:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Tips:
给定一个数组,该数组是由一个有序的数组经过旋转(即将前面一段数字接到整个数组之后)得到的。判断target是否存在于该数组之中。
数组中不存在重复数字,如果target存在,返回他的index不存在则返回-1.
思路:
查找某数字是否存在,可以使用二分查找,但是由于该数组不是正常的有序,不能使用正常的BS。
设置两个指针,low high分别指向数组的首尾,mid=(high+low)/2;旋转后的数组用mid分为俩半,必然有一半是有序的,先判断target是不是在有序的一半内,如果在直接进行二分搜索,如果不在那么就在另外一部分里面。
代码:
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return -1;
int len = nums.length ;
int low = 0;
int high = len - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target)
return mid;
if (nums[low] <= nums[mid]) {
if(target<nums[mid] && target>=nums[low]){
high=mid-1;
}else{
low=mid+1;
}
}else{
if(target>nums[mid] && target<=nums[high]){
low=mid+1;
}else
high=mid-1;
}
}
return -1;
}
【Leetcode】33. Search in Rotated Sorted Array的更多相关文章
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- 【Leetcode】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 【一天一道LeetCode】#33. Search in Rotated Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...
- 【LeetCode】033. Search in Rotated Sorted Array
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- Kafka学习之路 (三)Kafka的高可用
一.高可用的由来 1.1 为何需要Replication 在Kafka在0.8以前的版本中,是没有Replication的,一旦某一个Broker宕机,则其上所有的Partition数据都不可被消费, ...
- Odoo作为App后端时如何调试App
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9307340.html 一:Odoo可以作为app后台+后台管理系统使用 Odoo作为一个可供二次开发的框架, ...
- python 通过shutil实现文件后缀名判断及复制
In [18]: for file in os.listdir('.'): ...: if os.path.splitext(file)[1] == '.html': ...: print(file) ...
- QGIS(2.18.15 源码)+Qt(5/5.9.3)+VS2015(X64)编译
由于工作要求,今年需要基于Qt搞跨平台的GIS.前期未曾接触过Qt,最近也简单学习了下,开源的QGIS是非常不错的学习资源,下了最新版的QGIS源码,不过在VS2015下却没法直接打开.网上查了很多资 ...
- HDU1232
https://vjudge.net/problem/HDU-1232 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都 ...
- iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义)
iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义) 2017年9月20日,苹果如期推送 Xcode 9 和 iOS 11的更新. Xcode 9正式版与之前bet ...
- Dozer 使用小结
这篇文章是本人在阅读Dozer官方文档(5.5.1版本,官网已经一年多没更新了)的过程中,整理下来我认为比较基础的应用场景. 本文中提到的例子应该能覆盖JavaBean映射的大部分场景,希望对你有所帮 ...
- How develop BigData Project in Visual Studio
- #20155232《网络对抗》Exp9 Web安全基础
20155232<网络对抗>Exp9 Web安全基础 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 实验过程 WebGoat Webgoat是OWASP组织研 ...
- Node.js 下载路径/微软产品下载路径
https://nodejs.org/en/ https://www.microsoft.com/en-us/download //微软官方下载地址,可以下载VS2015 SQL 等 微软产品