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 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.

我突然想用英文解说下,展示一下我英文解说这么专业主题的功力O(∩_∩)O~,都搞双语的话好像又太费时间了。

This is a classic interview question. It's solution is similar with normal binary search. The only difference is that we need to add more conditional sentences.

和普通的二分法差不多

The key points is when we divide the array into two half, we need to compare A[mid] with one of the end element of the array, so that we can know which half of the array is sorted, and which half is rotated, and divided them again subsequently.

关键是会增加条件判断

The difference I do here is that I add one normal binary search here as  a helper function. Actually you don't need the binary search function, just one function will be alright. But that's fun to add them together to check the difference between them.

增加普通的binary search对比一下

class Solution {
public:
int search(int A[], int n, int target)
{
return unordBiSearch(A, 0, n-1, target);
} int unordBiSearch(int A[], int low, int up, int tar)
{
if (low > up) return -1; int mid = (low+up)>>1;
if (A[mid] == tar)
return mid;
if (A[mid]>A[up])
{
if (A[low] <= tar && A[mid] > tar)
return biSearch(A, low, mid-1, tar);
else return unordBiSearch(A, mid+1, up, tar);
}
if (A[mid]<A[up])
{
if (A[mid] < tar && A[up] >= tar)
return biSearch(A, mid+1, up, tar);
else return unordBiSearch(A, low, mid-1, tar);
}
return -1;
} int biSearch(int A[], int low, int up, int key)
{
if(low>up) return -1;
int mid = (low+up)>>1;
if (key < A[mid])
return biSearch(A, low, mid-1, key);
else if (A[mid] < key)
return biSearch(A, mid+1, up, key);
return mid;
}
};

LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找的更多相关文章

  1. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

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

  2. [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 ...

  3. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  4. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  5. [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 7might ...

  6. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

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

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

  8. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

  9. [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

    11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...

随机推荐

  1. 2015年9月29日 sql 触发器

    触发器(trigger):当有关联操作的时候使用(级联操作),属于ddl关键字. eg:下订单时,创建中的商品数量要减少:退票时,总的票数要增加.         在订单上建立触发器         ...

  2. Qt HTTP内部构架

    QUrl url("http://qt.gitorious.org"); QNetworkRequest request(url); QNetworkAccessManager m ...

  3. IntelliJ IDEA配置缓存地址

    idea的配置缓存路径是保存配置文件和插件的本地目录,对idea上的所有个人修改全都储存在那里 相比于eclipse的免安装.解压即用来说,一旦系统重装或者idea重装又没有进行备份的情况下,个人对i ...

  4. web服务编码设置

    response设置编码. <%@ page contentType="text/html;charset=UTF-8" %> 是服务器端java程序运行时的输出编码, ...

  5. 使用ImageMagick和Tesseract进行简单数字图像识别

    使用ImageMagick和Tesseract进行简单数字图像识别 由于直接使用 tesseract 进行识别,识别率很低, ImageMagick 安装.配置及使用: 平台:winXP 1. 安装I ...

  6. python之--条件判断和循环

    Python之判断 和其他语言一样,python同样具有条件判断和循环的操作,比如我们可以编写一个简单的判断操作:使用if关键字可以达到判断的效果,如下例: >>> test_if ...

  7. Unreal Engine4 蓝图入门

    微信公众号:UE交流学习    UE4开发群:344602753 蓝图是Unreal Engine的特点,用C++编程固然好,但是效率要低很多,主要是国内资料比较少,所以不太容易学习,用蓝图编程可以节 ...

  8. 单位有b\B\K\M\G的相互转换

    计算机存储计量单位 1. 计算机最小存储计量单位是:BIT(位) 2. 计算机最基本存储计量单位是:Bytes(字节) 3. Bit和Bytes的关系:8Bit=1Bytes 4. 其他常用单位:1K ...

  9. C# 读取网络txt文件 并写在本地txt文件中

    public void WriteStream() { try { stirng ImagesPath = ImagesPath + "\\2013-09-27.txt"; Htt ...

  10. 关于Json传递的日期/Date(数字)/解析

    在将DateTime类型的数据Json后传到前台展示,出现如下效果 ,在客户端如何解析呢?在jquery easyui 的字段中加一个格式化的函数调用.   { field: 'CreateTime' ...