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 ...
随机推荐
- Oracle 11g安装图文攻略
一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...
- Makefile学习之路5——通过函数增强功能
通过函数能显著增强Makefile的功能.对于simple项目的Makefile,尽管使用了模式规则,但还是有一件比较麻烦的事情,就是要在Makefile中指明每一个项目源文件.下面介绍几个后期会使用 ...
- 深入学习HttpClient(一)扩展额外的功能
HttpClient作为.net4.5新增的Http库除了对于async/await形式的异步支持外,还向我们展示了其强大的扩展能力. [类库的设计] 让我们先看下Httpclient的设计图: 图中 ...
- C#里面的枚举与位或运算符 一个枚举变量存入多个值
以前我们如果定义一个枚举类型 ,然后定义一个枚举变量,那么这个枚举变量只能为类型中的一个值,现在我们想要一个变量能够代表多个值: 今天看<Pro Net 2.0 Windows Forms An ...
- scala中计算的的一个小问题,超出Int.maxValue时不会报错
如果小于Int.max时他不会报错 def sumcount(str:String): Int ={ val ints = for(c <- str)yield { println(c.asIn ...
- [debootstrap]制作基于arm平台的debian文件系统
之前用过的Linux文件系统是直接busybox制作的,而当前使用的是debian的Jessie,看了一些博客后,了解到如果使用debian,那么直接使用debootstrap来构建文件系统即可 -- ...
- Ubuntu 11.04 下安装配置 JDK 7
第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...
- 使用info命令查看Redis信息和状态
redis-cli连接服务器后,使用info命令查看Redis信息和状态: ? 1 info 其中memory段显示了redis的内存使用状态. 以下内容复制自:http://redisdoc.com ...
- jQuery中ajax的使用与缓存问题的解决方法
http://www.jb51.net/article/44620.htm —————————————————————————————————————————————————————————————— ...
- Javascript 验证上传图片大小[客户端验证]
需求分析: 在做上传图片的时候,如果不限制上传图片大小,后果非常的严重.那么我们怎样才可以解决一个棘手的问题呢?有两种方式: 1)后台处理: 也就是AJAX POST提交到后台,把图片上传到服务器上, ...