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 ...
随机推荐
- response小结(四)
1.发送http头,控制浏览器定时刷新网页(REFRESH). package com.yyz.response; import java.io.IOException; import javax.s ...
- Excel数据生成Sql语句的方法
选中想要生成的列,套用表格格式,选中表包含标题的选项确定,然后在最右边的一列第二行处,点击函数功能,选择CONCATENATE,在文本里输入想要的结构即可 代码如下 复制代码 ,=CONCATENA ...
- plsqldev与sqldeveloper
plsqldev连接 1.连接不同服务器,要修改tnsnames.ora文件,具体如下修改如下位置 # tnsnames.ora Network Configuration File: \app\us ...
- iOS 字体滚动效果 ScrollLabel
写了一个简单的字体滚动效果. 用了一种取巧的方式,传入两个一摸一样的Label(当然也可以是别的视图), 话不多说,代码里面讲解. SEScrollLabel.h #import <UIKit/ ...
- windows 7 64 bit 使用 virtual box 的经验
本人电脑是联想thinkpad E535的机子,安装的是64bitwindows7 旗舰版 为了更好的工作,我安装了虚拟机virtualbox最新版 很不幸,我出现了多次蓝屏的情况,我换到32位系统下 ...
- (转)使用CruiseControl+SVN+ANT实现持续集成之三
在上一节中我们介绍了环境搭建和配置介绍,并快速启动CC查看集成结果,在本节中我们将详细介绍CC构建操作及监视. 1. 启动CC服务器 通过执行其根目录下的cruisecontrol.bat文件来启动C ...
- 学习笔记--Quartz定时器
文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/338.html 1.Quartz定时器执行流程 Quartz需要定义执行任务.触发器,在 ...
- L012-oldboy-mysql-dba-lesson12
L012-oldboy-mysql-dba-lesson12 graphite监控mysql可以达到秒级别 Zabbix for mysql NagiosXI daemontools linux ...
- mac 上的版本控制工具SmartSVN9.0.4(破解版)
附带上破解版安装说明: 1.在MAC上选中SmartSVN.dmg,右键->打开2.双击syntevo_keygen.jar 如果没有安装java会自动提示安装的3.输入Name Email(随 ...
- 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。
看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...