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 ...
随机推荐
- Jersey(1.19.1) - Use of @Context
Previous sections have introduced the use of @Context. The JAX-RS specification presents all the sta ...
- Android项目源码界面超级华丽的仿QQ最新版本
这是一个我们比较熟悉的一款应用,高仿专仿最新QQ应用源码,也是一个高仿QQ最新版本的项目,界面超级华丽,使用了大量的自定义控件,项目里实现了部分功能,例如WIFI-FTP(把手机变成FTP服务端,可以 ...
- Linux c字符串中不可打印字符转换成16进制
本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的 ...
- 学习Learn Python The Hard Way 前言中的一段话,可与君共勉
在你通过这本书学习编程时,我正在学习弹吉他.我每天至少训练 2 小时,至少花一个小时练习音阶.和声.和琶音,剩下的时间用来学习音乐理论和歌曲演奏以及训练听力等.有时我一天会花 8 个小时来练习,因为我 ...
- UMeng 友盟的用户数,启动数 等
最近一哥们研究UMeng 友盟的用户数,启动数,设备和位置相关数据,发现真的可以模拟啊. 支持Android,IOS,WPhone平台,同时可以实现每小时按10万级的速度增加,启动次数也是可以按10万 ...
- Java垃圾回收介绍(译)
在Java中,对象内存空间的分配与回收是由JVM中的垃圾回收进程自动完成的.与C语言不同的是,在Java中开发者不需要专门为垃圾回收写代码.这是使Java流行的众多特征之一,也帮助了程序员写出了更好的 ...
- IOS-UI-UIDynamic(一)
UIDynamic是从iOS7开始引入的技术 属于UIkit框架 可以模拟显示生活中的物理现象 如碰撞 抖动 摆动等 一. 使用UIDynamic步骤: 1.创建一个动力效果器UIDynamicAni ...
- spring自定义注解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- (转)Salesforce的440亿美金并购宣告企业软件市场进入3.0互联网化时代
导语:Salesforce代表着“移动+云”时代企业软件领域新的架构和商业模式的颠覆者.企业软件转向“移动+云”架构,将极大改变传统企业IT市场的格局…… 近期一则新闻极大的刺激了企业软件市场的神经, ...
- less使用001
官网: http://lesscss.org/ . 中文文档直接百度搜索less,能找到N多网站提供的支持. less-gui,使用国产的koala, 其中文帮助文档地址. 拖拽一个目录到考拉就新建了 ...