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

    If a JMS provider detects a problem with a connection, it will inform the connection’s ExceptionList ...

  2. Linux 命令 - fg & bg: 将进程切换到前台(后台)运行

    后台运行的进程不会受到任何键盘的影响,包括试图用来中断它的 Ctrl-C 键.想要使得进程返回到前台来运行,可以使用 fg 命令来实现. 可以通过在 fg 命令后面加上百分比符号和作业编号(称为 jo ...

  3. oracle视图索引

    reate table fleet_header(  day date,name varchar2(20),  route_id number(5),fleet_id number(5)); crea ...

  4. 第六篇、AVplayer定制视频播放控件

    1.引用头文件#import AVFoundation 2.自定义AVPlayer(播放的机器) 3.自定义AVPlayerItem(胶片) >> 视频的URL转成AVAsset 4.AV ...

  5. java新手笔记34 连接数据库

    1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...

  6. java实现的一个maven多模块项目自动生成工具

    平时在做spring mvc web新项目时,都需要自己去搭建spring mvc的项目框架,包括基本pom 依赖引入,基本配置文件(web.xml,spring-mvc.xml,数据库配置文件等等) ...

  7. spring aop配置文档部分翻译

    欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...

  8. int组成时间值

    int startYear = int.Parse(DateTime.Now.Year.ToString()); int startMouth = int.Parse(DateTime.Now.Mon ...

  9. rolling hash

    也是需要查看,然后修改,rolling hash, recursive hash, polynomial hash, double hash.如果一次不够,那就2次.需要在准备一个线段树,基本的线段树 ...

  10. angularJs--<ui-select>

    显示: ng-model里面的变量 如果为1,会去 ui-select-choices 里找这个idea数组,然后把数组对应name字段的值,交给 ui-select-match里面显示,$selec ...