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
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
三个招:前两个很简单,巧妙地是第三个。
//方法一:分批次二分查找 ;查找出分割点需要时间O(n),总时间复杂度 O(n)
int BinaryResearch(int A[],int low,int high,int target)
{
int l = low;
int h = high;
while(l<=h)
{
int mid = (int)((h+l)/2);
if(A[mid]==target) return mid;
else if(A[mid]<target) l = mid+1;
else h = mid-1;
}
return A[h];
}
int search1(int A[], int n, int target) { int index = 0;
for(int i = 0;i<n-1;i++)
{
if(A[i]>A[i+1])
{
index = i;
break;
}
}
int a = BinaryResearch(A,0,index,target);
int b = BinaryResearch(A,index+1,n-1,target);
if(a==-1&&b==-1)
return -1;
else
return a==-1?b:a;
}
int search2(int A[], int n, int target) {
//顺序查找 ,O(n)
int index = -1;
for(int i = 0;i<n;i++)
{
if(A[i]==target)
{
index = i;
}}
return index;
}
//完全的二分查找,O(logn)
int search3(int A[], int n, int target) {
int left = 0;
int right = n-1;
while(left<=right)
{
int mid = (int)((left + right)/2);
if(A[mid] == target) return mid;
if(A[left]<A[mid])//A[mid]在前半部分
{
if(target<A[mid]&&target>=A[left])
right = mid-1;
else left = mid+1;
}
else if(A[left]>A[mid])//A[mid]位于后半段
{
if(target>A[mid]&&target<=A[right])
left = mid+1;
else
right = mid-1;
}
else left++;
}
return -1; }
LeetCode_Search in Rotated Sorted Array的更多相关文章
- leetcode_Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [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 ...
- 【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 ...
- LintCode Find Minimum In Rotated Sorted Array
1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...
- LeetCode-Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- Leetcode Find Minimum in Rotated Sorted Array I and II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- hive优化总结
一.表设计 合理分表 合理设计表分区,静态分区.动态分区 二.扫描相关 1.谓词下推(Predicate Push Down) 2.列裁剪(Column Pruning) 在读数据的时候,只关心感兴趣 ...
- github上搭建网站前台页面
其实就是把html页面提交到github,为了能在线演示: 1. 首先在github网站找到你的项目 2. 点击设置 3. 找到这几个选项,选择master branch打钩,然后保存 4. 然后就会 ...
- VBA学习笔记(1)----VBA对象属性方法
'VBA对象 'VBA中的对象其实就是我们操作的具有方法.属性的excel中支持的对象 'Excel中的几个常用对象表示方法 '1.工作簿 ' Workbooks 代表工作簿集合,所有的工作簿,Wor ...
- Android App性能測试
一.内存 1.查看单个应用App最大内存限制 Command:adb shell "getprop|grep heapgrowthlimit" C:\Users\hujiachun ...
- Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!
各位哥哥姐姐弟弟妹妹小伙伴们春节好! 2014年对于我们程序员很重要, 因为今年是 "码" 年! SS在此重要之年到来之际热力推出两大重要功能恭贺新春: 1. 浏览器视图 以前SS ...
- 使用Javascript实现随机字符串
方法一(其实是毫秒时间数字字符串): function randomString() { return '' + new Date().getTime(); } 方法二(随机字母数字字符串): var ...
- TensorFlow基础笔记(0) 参考资源学习文档
1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...
- 在堆栈中,push为入栈操作,pop为出栈操作
LinkedList提供以下方法:(ArrayList无此类方法) addFirst(); removeFirst(); addLast(); removeLast(); 在堆栈中,push为入栈操作 ...
- 【BZOJ】1621: [Usaco2008 Open]Roads Around The Farm分岔路口(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1621 这题用笔推一下就懂了的.... 当2|(n-k)时,才能分,否则不能分. 那么dfs即可.. ...
- XMPP客户端
1. Strophe.js 2. Converse.js