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. Mysql修改设置root密码的命令及方法

    方法一:使用SQL语句命令UPDATE 需用到Mysql自带的加密函数PASSWORD(string),该函数对一个明文密码进行加密,但不能解密.专门用于mysql.user(用户权限表)中设置密码, ...

  2. MVC + JQUERY + AJAX

  3. Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”

    Cocos2d-x 3.0 Alpha 1开始 对目录结构进行了整合.结果有些附加项目也被在项目中被精简出去. 比如说如果你需要使用CocoStdio导出的JSON.或使用Extensions扩展库, ...

  4. 在游戏中使用keybd_event的问题

    转自在游戏中使用keybd_event的问题 今天发现在游戏中,keybd_event不能使用,结果发现游戏是使用directinput实现读取键盘的,关键还是扫描码的问题,我抄了一段老外的代码,经测 ...

  5. Android-x86虚拟机安装配置全攻略

    转自Android-x86虚拟机安装配置全攻略 注:这里安装从简,具体请参考虚拟机Vmware安装运行安卓4.0详细教程 Android-x86虚拟机安装配置网上有很多,但是全部说明白的确不多,希望这 ...

  6. 【踩坑记】从HybridApp到ReactNative

    前言 随着移动互联网的兴起,Webapp开始大行其道.大概在15年下半年的时候我接触到了HybridApp.因为当时还没毕业嘛,所以并不清楚自己未来的方向,所以就投入了HybridApp的怀抱. Hy ...

  7. hadoop多机安装YARN

    hadoop伪分布安装称为测试环境安装,多机分布称为生成环境安装.以下安装没有进行HA(热备)和Federation(联邦).除非是性能需要,否则没必要安装Federation,HA可以一试,涉及到Z ...

  8. java的版本区别、下载、配置

    参考:http://blog.csdn.net/liujun13579/article/details/7684604 java的版本区别 常用的 java 程序分为  Java SE.Java EE ...

  9. 深入浅出 - Android系统移植与平台开发(十)- Android编译系统与定制Android平台系统(瘋耔修改篇二)

    第四章.Android编译系统与定制Android平台系统 4.1Android编译系统 Android的源码由几十万个文件构成,这些文件之间有的相互依赖,有的又相互独立,它们按功能或类型又被放到不同 ...

  10. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...