思路,这道题用二分,唯一的不同就是,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. ecshop 默认图处理

    function get_banner_path($img) { $img = empty($img) ? C('no_picture') : $img; return $img;}

  2. nginx配置图片防盗链

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 30d; access_log off; valid_referers none blocked ...

  3. Windwos下常用DOS命令

    1.添加用户命令: net user 用户名 密码 /add 2.将用户加入组的命令: net localgroup administrators 用户名 /add 3.在dos命令行模式下启用用户: ...

  4. verilog阻塞与非阻塞的初步理解(三)

    下面这段源码是因为习惯不好,出现不正确波形的例子. module pwm_division(reset,clkin,clkout); input reset,clkin; output clkout; ...

  5. verilog阻塞与非阻塞的初步理解(二)

    将阻塞模块改为下述代码: module blocking(clk,a,b,c); :] a; input clk; :] b,c; :] b,c; always @(posedge clk) begi ...

  6. Java 8 Optional类深度解析

    身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只有在非空的前提下才能将其作为其他方法的参数. ...

  7. linux下定时执行任务方法【转】

     之前就转过一篇关于定时任务的文章,前俩天用,还的翻出来看!!!再转一次,备用,,需要的时候不用麻烦找! ----------------------------------------------- ...

  8. jqueryui / accordion的用法记录

    jqueryui 的 widget 中包含了基本上我们都需要的ui组件, 除了那个unslider. 参考地址是: www.jqueryui.com. 要能够看懂/并使用/ 完全掌握的话, 就要使用其 ...

  9. List之Union(),Intersect(),Except()

    http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800202.html List之Union(),Intersect(),Except() ...

  10. requests的安装与简单运用

    requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: python的标准库urllib ...