题目: Suppose a sorted array 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.

就是说,排序数组可能是右移了一定位数。让你在这个数组中找一个target值。当然用线性查找就没意义了。

想这个解法需要头脑比较清晰。记得原来高中做题,老师最常说的一句话是啥?”要揣摩出题人意图啊“。。。这道题也是这样的。如果是在一个有序数组里面找一个值,那么一般都是用binarySearch。现在数组变了,(当然,还保持一些其他特点,我们下面说),能不能用binarySearch呢,或者我们改一下binarySearch呢?

如果用binarySearch,我们在通过Low和High序号得到Mid以后,应该如何剔除一半的数据呢?

下面是rotate后的一个有序数组的图。四边形的斜边表示数组中数值的大小。

在这种情况下数组分了两部分,分别都是有序(递增)的。

当我们计算了Mid以后,有两种可能,分别用Mid1和Mid2表示。

1. 如果A[Low] < A[Mid],说明Mid落在区间1中,即图中Mid1的位置。那么,如果target小于A[Mid1],那么继续在Low和Mid1中间搜索;否则,在Mid1和High中间搜索;

2. 如果A[Low] >= A[Mid],说明Mid落在区间2中,即图中Mid2的位置。同理,如果target小于A[Mid2],那么继续在Low和Mid2中间搜索;否则,在Mid2和High中间搜索。

这样,平均地,我们每次剔除一半数据,时间复杂度是O(logn)。

代码如下:

 private static int search2(int[] A, int target){
int lo = 0;
int hi = A.length - 1;
while(lo <= hi){
int mid = lo + (hi - lo)/2;
if(target == A[mid]) return mid;
if(A[mid] > lo){
if(target >= lo && target < A[mid]){
hi = mid - 1;
}else {
lo = mid + 1;
}
}else {
if(target <= hi && target > A[mid]){
lo = mid + 1;
}else {
hi = mid -1;
}
}
}
return -1;
}

LeetCode 笔记系列九 Search in Rotated Sorted Array的更多相关文章

  1. leetcode第32题--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 ...

  2. LeetCode(33)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 m ...

  3. leetcode个人题解——#33 Search in Rotated Sorted Array

    思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...

  4. Leetcode系列-Search in Rotated Sorted Array

    做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...

  5. [Leetcode] Search in Rotated Sorted Array 系列

    Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...

  6. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  7. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  8. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. 修改host简化远程访问

    问题描述: 使用本机登陆服务器时,需要经常输入IP地址,在局域网下和非局域网下输入的IP又不一样,十分麻烦,如果可以给IP命名一个简单的名字,岂不是很方便? 解决方法: 修改host文件: vim / ...

  2. FTL页面常用到的一些方法combobox、combotree、datagrid

    参考文件:点击下载 1.combobox: (1).js 1)初始化combobox //相似度 $('#same').combobox({ //url:"<@s.url value= ...

  3. HttpOperater-模拟HTTP操作类

    using System; using System.IO; using System.Linq; using System.Net; using System.Text; using System. ...

  4. putty自带工具plink自动登陆ssh

    PLINK.EXE -C -N -D 127.0.0.1:7000 root@111.111.111.111  -pw 123456 解释成中文: PLINK.EXE -启用数据压缩 -不要shell ...

  5. C strlen vs sizeof

    strlen是函数. sizeof是操作符..以下一个简单的程序说明这件事: #include <iostream> #include <string.h> using nam ...

  6. location ^~ /images/

    } location ^~ /images/ { root /static/; } #当匹配到/images/ 开头的uri 会把网站定位到/static/下,并且不在向下继续匹配!!! 注意: ^~ ...

  7. The Report Of Twisted’s Death or: Why Twisted and Tornado Are Relevant In The Asyncio Age

    Speech on PyCon2016 https://www.youtube.com/watch?v=82vuCZ4FLFE

  8. weblogic线程阻塞性能调优(图解)转

    声明:出现这个问题有程序方面.网络方面.weblogic设置方面等等原因,此文章主要讲述由于weblogic设置而导致的解决办法. 因为: 1.程序问题,需要项目自己去解决,weblogic在做优化处 ...

  9. 百度地图 JSAPI使用 mark 定位地址 与周边覆盖物

    http://lbsyun.baidu.com/index.php?title=jspopular   api  http://developer.baidu.com/map/jsdemo.htm#a ...

  10. linux服务器 IE中ico 不能正常显示

    问题: mime_type: image/vnd.microsoft.icon 的,但发现在 IE 下面,直接打开 icon 的地址,图标不能正常显示 1.将ico放在windows服务器上,直接访问 ...