Searching in a rotated and sorted array
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的更多相关文章
- 【题解】【数组】【查找】【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 ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
- [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: / ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- 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 ...
- 【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 ...
随机推荐
- Django学习--9 Admin
1.vim settings.py 打开 'django.contrib.admin' vim urls.py 打开 from django.contrib import admin (注意 ...
- JavaScript 参考手册
http://www.w3school.com.cn/jsref/index.asp https://developer.mozilla.org/zh-CN/docs/Web/JavaScript
- Centos6.5 64linux系统基础优化(一)
1 SecureCRT配色方案 http://blog.csdn.net/zklth/article/details/8937905 2 32位和64位Centos linux系统的区别及实际查看 ...
- 关于webview嵌入swf
有的机子CPU不支持swf播放的,不知道你是不是中奖了.. webview 加载swf很简单: if(versionDouble>=2.2){//版本低于2.2的系统无法播放sw ...
- DEDECMS中,常见全局变量
全局变量 {dede:global.cfg_cmspath/} 是dedecms 的安装目录,一般就是网站的根目录. {dede:global.cfg_cmsurl/}是当前目录 注意加一根斜线{de ...
- python 自动化之路 day 00 目录
目录 初识Python Python基本数据类型 Python基础之函数 Python基础之杂货铺 模块 面向对象 网络编程 HTML CSS JavaScript DOM jQuery Web框架本 ...
- L010-oldboy-mysql-dba-lesson10
L010-oldboy-mysql-dba-lesson10 来自为知笔记(Wiz)
- Less和Sass编译
使用koala编译 Koala 是一款由国人开发的开源预处理语言图形编译工具,目前已支持 Less.Sass.Compass 与CoffeeScript. 目前支持以下系统:Windows,Mac, ...
- 一款js控制背景图片平铺
背景图片的平铺方法有很多种,纯色背景,渐变背景,图片背景,今天讲的是移动端的图片背景~~~~ <style> html,body{;;} .body{background: url(ima ...
- 粗谈Android中的对齐
在谈这个之前先啰嗦几个概念. 基线:书写英语单词时为了规范书写会设有四条线,从上至下第三条就是基线.基线对齐主要是为了两个控件中显示的英文单词的基线对齐,如下所示: Start:在看API的时候经常会 ...