Given a sorted array that has been rotated serveral times. Write code to find an element in this array. You may assume that the array was originally sorted in increasing order.

思路:first to know that to ratate an array multiple times is no different than to rotate it just once.

No matter how the array has been rotated, it would fall into 3 kinds of situations below:

#1: If array[low] < array[mid], then left side is ordered normally.

#2: If array[mid] < array[high], then right side is ordered normally.

#3: array[mid] = array[low] = array[high], this might occur only when duplicates are allowed. In this condition, we have to search both sides because we don't know which side is ordered normally.

Note: a little tricky thing is that when array has exactly 2 elements, the above logic may go wrong, so just consider this situation separately.

Searching in a rotated and sorted array的更多相关文章

  1. 【题解】【数组】【查找】【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 ...

  2. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  3. [Algorithm] How many times is a sorted array rotated?

    Given a sorted array, for example: // [2,5,6,8,11,12,15,18] Then we rotated it 1 time, it becomes: / ...

  4. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  5. [LeetCode] Find Minimum 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 ...

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

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

  9. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

随机推荐

  1. Spring(3.2.3) - Beans(10): 生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...

  2. Git CMD - push: Update remote refs along with associated objects

    命令格式 git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pac ...

  3. sqoop-1.4.6安装配置

    1. 下载sqoop-1.4.6并解压 wget http://archive.apache.org/dist/sqoop/1.4.6/sqoop-1.4.6.bin__hadoop-2.0.4-al ...

  4. python学习day5--set、函数

    1.set 无序,不重复序列 创建:与dict一样用{},区别在于dict内元素为键值对 se={"123","456,444"} print(type(se) ...

  5. 创建Mysql 序列

    create table sequence( name ) not null primary key, current_value , increment , max_value BIGINT, -- ...

  6. KVC/KVO总结

    KVC(键值编码) 动态设置: setValue:属性值 forKey:属性名(用于简单路径) setValue:属性值 forKeyPath:属(用于复合路径,例如Person有一个Account类 ...

  7. iMAC——全新重装Mac系统

    在参考网上重装Mac系统教程的时候,感觉这篇教程挺不错: http://www.iplaysoft.com/osx-yosemite-usb-install-drive.html (此教程终端命令处需 ...

  8. UIView-4-EventForViews(在view上加入button时候的事件处理)

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  9. (转)推荐一个在Linux/Unix上架设ASP.NET的 WEB服务器--Jexus

    在Linux/Unix上架设ASP.NET WEB服务器,有两个可选方式,一种是Mono+XSP,一种是Mono+Jexus,其它的方式,比如 Apache+mod_mono.Nginx+FastCg ...

  10. 问题解决:引入com.sun.management.OperatingSystemMXBean 出错

    不能import  com.sun.management.OperatingSystemMXBean 解决: Eclipse默认把这些受访问限制的API设成了ERROR.只要把Windows-Pref ...