思路,这道题用二分,唯一的不同就是,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)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  4. [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. ...

  5. [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 ...

  6. 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. ...

  7. [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 ...

  8. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  9. Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置

    在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...

随机推荐

  1. CSS实时编辑显示

    方法 CSS实时编辑显示:通过display:block让css文本显示出来,再加上contentEditable使文本可编辑 <!DOCTYPE html> <html> & ...

  2. python 多线程学习

    多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...

  3. CSS3新增基础属性总结——20160409(易达客)

    1.box-shadow :h-shadow v-shadow blur spread color inset(outset) h-shadow:必须:水平阴影位置,允许负值. v-shadow:必须 ...

  4. PetaPoco dynamic

    PetaPoco最初的灵感来自Massive-通过dynamic Expando objects返回一切.对于大多数情况我觉得这比较麻烦,更喜欢强类型的类.但是有些时候支持dynamic也是有用的-特 ...

  5. OC-苹果官方文档

    苹果官方文档 help>documentation>左边iOS>language>objective>the objective-c programming langua ...

  6. (转载)ecshop制作成手机网站的方法

    ecshop用手机访问的时候,会自动跳转到  /mobile 目录下,ecshop自带的wap模板是用wml制作的,如果按这种情况,又需要制作一套模板,太麻烦,现在都是智能手机时代,wml模板已经不能 ...

  7. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力

    [摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...

  8. php去掉字符串的最后一个字符附substr()的用法

    转自:http://www.jb51.net/article/26604.htm 今天项目中用到,去掉字符串中的最后一个字符 原字符串1,2,3,4,5,6, 去掉最后一个字符"," ...

  9. Nginx中的rewrite指令

    转自:http://www.76ku.cn/articles/archives/317 rewite.在server块下,会优先执行rewrite部分,然后才会去匹配location块server中的 ...

  10. Javascript 中我很想说说的 this

    this是每一个想要深入学习Javascript的人必过的一关,我为this看过很多书查过很多资料,虽然对this有了一定的了解并且也经常使用this,但是如果有人问我  this是什么呀? 我依旧不 ...