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. JAVA NIO之Character Set

    明白以下几个概念: 字母集(Character Set),汉字,特殊符号,字母这些都是字符集: 字符编码集(Coded character set),将字符集的字符使用数字进行编码:比如ASCII,就 ...

  2. jquery插件的写法

    jquery插件及zepto插件,写法上有些区别. 区别点: 1.自定义事件的命名空间 jq的时间命名空间是用点“.”,而zepto是用冒号“:” 如 //jquery $(this).trigger ...

  3. 多个div独立控制其显示/隐藏

    今天要说一个神奇的html标签op,静态页下可以配合jquery分别控制每个层的显示/隐藏切换. 如果用动态中使用,用文章id做区分就可以了. <html> <head> &l ...

  4. 一个奇怪的编码 big5-hkscs

    # --*-- coding:utf-8 --*-- import urllib2 import urllib postDict = { 'IsExist_Slt_Part_Id': 'False', ...

  5. Java语言基础(六)char成员变量默认初始值 最简单的Java源文件 Java的main()方法

    ①char成员变量的初始值是:'\u0000' ②package用来指定该文件所处的包的名称,必须位于源文件的顶端. import java.util.*; package com.hyy.test; ...

  6. QWidget属性,函数的学习

    我把所有属性重新按功能排了一遍,这样才能灌到自己脑子里,并且方便自己以后查找: -------------------- 颜色/渲染方式 -----------------------QWidget: ...

  7. C#实例:5个.net经典例子(窗体与界面设计)

    实例001  带历史信息的菜单 实例说明 在开发图纸管理软件时,要求在菜单上记录用户最近打开的档案或图纸,以方便下次使用.如图1.1所示,单击“文件”菜单下的“打开文件”子菜单,打开需要查阅的图纸.下 ...

  8. (转载)AS3.0实例学习 熟悉新的事件机制和addChild的运用

    (转载)http://www.jb51.net/article/13139.htm 首先声明:本人大菜鸟一个,刚接触AS3不久,许多理念还没来得及灌输,这些case都是从网上down的,但因为解说是英 ...

  9. POJ 3107

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  10. NHibernate加载DLL错误

    这几天在开发关于Rest的服务,其中用到了NHibernate来进行数据库交互,突然有一天发现了一个错误,如下: Could not load file or assembly 'NHibernate ...