11.3---旋转有序数组之后查找元素(CC150)
思路,这道题用二分,唯一的不同就是,1,a[left]<a[mid]。那么说明左右有序,如果key还在a[left],a[mid]之间,就在这里找,如果不在就在右边找。注意:这里<要改成<=才对。
2,如果不是条件一,那么就是右边有序,看key是否在mid和right之间,在的话这个范围找,不在的话,左边找。
答案:
public static int findElement(int[] a, int n,int key){
int left = 0;
int right = a.length - 1;
while(left <= right){
int mid = (left + right) / 2;
if(key == a[mid]) return mid;
if(a[left] <= a[mid]){
if(key <= a[mid] && key >= a[left]){
right = mid -1;
}
else{
left = mid + 1;
}
}
else{
if(key > a[mid] && key < a[right]){
left = mid + 1;
}
else{
right = mid -1;
}
}
}
return -1;
}
11.3---旋转有序数组之后查找元素(CC150)的更多相关文章
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- [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算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- [LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置
在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...
随机推荐
- 如何查看crontab的日志记录
在Unix和类Unix的操作系统之中,crontab命令常用于设置周期性被执行的指令,也可以理解为设置定时任务. crontab中的定时任务有时候没有成功执行,什么原因呢?这时就需要去日志里去分析一下 ...
- python线程锁
import time,threading balance = 0 lock = threading.Lock() def change_it(n): global balance balance = ...
- App Store
App store最新审核标准(2015.3)公布 http://www.cnii.com.cn/mobileinternet/2015-03/24/content_1550301.htm iOS提交 ...
- java中的各个数据结构区别
ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢 ...
- 简述frame、bounds、center
frame是该视图在父视图坐标系统中的位置和大小.(参照父视图中的坐标系统) bounds是该视图在自身坐标系统中的位置和大小.(参照自身的坐标系统) center是该视图的中心点在父视图坐标系统中的 ...
- Owin是什么?
OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平台的开放Web接口. 那Web接口是谁和谁之间的接口呢?是 ...
- DOS批处理中%cd%和%~dp0的区别
DOS批处理中%cd%和%~dp0的区别 在DOS的批处理中,有时候需要知道当前的路径. 在DOS中,有两个环境变量可以跟当前路径有关,一个是%cd%, 一个是%~dp0. 这两个变量 ...
- 【9-20】vimtutor学习笔记
第一节 ghjk移动光标 :q!:强制退出vim x:删除光标处的字符 i:在光标处插入 A:附加文本 :wq:保存文档并退出 第二节 dw:删除一个单词 d$:删除至行尾 de:删除光标处到该单词结 ...
- 如何在本地配置php分析工具xhprof
测试环境: linuxMint + nginx1.4.6+mysql5.5+php5.5 什么是xhprof? XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞时 ...
- Spring与Quartz的整合实现定时任务调度(转)
源:http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少 ...