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 ...
随机推荐
- nginx的简单操作
1.今天在nginx下访问403,发现图片访问出现403,其他没有问题,看来下图片的文件夹没有读写的权限,修改下权限就OK了! 2.重启nginx 在env/nginx/sbin目录下输入:nginx ...
- macbook pro的usb串口失效的的处理方法
macbook pro的usb串口失效的的处理方法 2011-08-24 12:14:32| 分类: mac|举报|字号 订阅 今天开电脑,无端端一个usb的串口失效了,接入鼠标 iphon ...
- 配置git密钥,然后新建仓库
Generating SSH keys (打开下面的链接) https://help.github.com/articles/generating-ssh-keys/ 完成配置后 开始在github上 ...
- Database Schema Reader
数据架构与INSERT脚本生成 https://dbschemareader.codeplex.com/wikipage?title=Writing%20Data&referringTitle ...
- MyEclipse 中各种 libraries 的含义
MyEclipse 中各种 libraries 的含义 JRE System Library,Java EE 5 Libraries,Referenced Libraries这三个都是各 ...
- python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2
File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...
- vim ctags使用方法
一.用好系统自带软件ctags 大部分的unix系统都有ctags软件,它能跟vim很好地合作. 用途: 生成c语言的标签文件,实现相关c文件之间的跳转. 用法: 1.生成标签文件 ...
- 在linux命令行中直接执行php命令
有时候用浏览器调试太麻烦,想在linux命令下直接执行php代码 php -r 'echo 0500;'
- C++你不知道的那些事儿—C++语言的15个晦涩特性
这个列表收集了 C++ 语言的一些晦涩(Obscure)特性,是我经年累月研究这门语言的各个方面收集起来的.C++非常庞大,我总是能学到一些新知识.即使你对C++已了如指掌,也希望你能从列表中学到一些 ...
- 隐藏NavigationBar时的一个坑
http://www.jianshu.com/p/efb960fed457 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: ...