Search in Rotated Sorted Array——LeetCode
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.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
题目大意:有序数组旋转,寻找其中是否有某个特定的值,时间复杂度O(log n)。
思路:旋转后的数组,旋转后的后一部分肯定比前一部分所有元素都小;所以数组从中间分开,至少有一半是有序的。
那么可以得到,如果中间的元素(也就是nums[mid])比nums[0]大,那么前半部分有序,如果中间的元素比nums[len-1]小,那么后半部分有序,这两种情况只会出现一种。这样就可以先看这个数在不在有序的这一半数组中,在的话没什么好说的,二分查找;不在的话,继续找另一半RotatedArray。思路就是这么个思路,然而想把题目A掉还要考虑很多细节,尤其是边界值,> or >= ,< or <=等等,极易出错。
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int low = 0, high = nums.length - 1;
while (low <= high) {
int mid = (low + high) >> 1;
if (nums[mid] == target) {
return mid;
}
if (nums[low] <= nums[mid]) {
if (target >= nums[low] && target <= nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (target >= nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
Search in Rotated Sorted Array——LeetCode的更多相关文章
- Search in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- search in rotated sorted array leetcode
原题链接 题意:给你一个目标值,或者返回其在数组中的下标位置,或者返回-1(表示不存在,查找失败). 例如 0 1 2 4 5 6 7 可能成为 4 5 6 7 0 1 2. 思路分析: 用二分搜索来 ...
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- [LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...
- Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
随机推荐
- ArrayList中进行删除操作引发的问题
1.普通for遍历 for(int i=0;i<list.size();i++){ if(list.get(i).equals("a")) list.remove(i); } ...
- Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性
为ImageButton自定义IconSource和Contents属性 xaml代码 <UserControl x:Class="SilverlightCreate.Silverli ...
- Mysql 游标的定义与使用方式
创建游标: 首先在MySql中创建一张数据表: CREATE TABLE IF NOT EXISTS `store` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...
- Linux证书登录,禁用密码
如果验证成功的话就可以关闭密码登陆方式了, 编辑/etc/ssh/sshd_config, 将PasswordAuthentication改为no, ChallengeResponseAuthenti ...
- container_of 和 offsetof 宏详解
在linux内核链表中,会遇到两个宏. 在include/linux/stddef.h中,有这样的定义 #define offsetof(TYPE, MEMBER) ((size_t) &(( ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- html和Url转码与解码
JS (JQuery)对Html.URL的编码与解码 首先引入JQuery文件 1.js对Html编码 function htmlEncode(value){ return $('<div/&g ...
- jeecg3.8在子表页面中使用WebUploader组件
bcAbout-update.jsp改动如下: 因为默认子表的上传组件不能回显,所以改造成WebUploader 1.在更新页面注销掉生成代码 <%--<script type=" ...
- hdu 3586 最小代价切断根与叶子联系
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 http://blog.csdn.net/woshi250hua/article/details ...
- 特殊的流程控制语句break continue exit
break语句可以结束当前的for.foreach.while.do-while.或者switch的执行. for($i=1; $i<10; $i++) { if($i == 5) { echo ...