Leetcode--Search in Rotated Sorted Array(旋转数组的查找)
2014-03-26 18:34
453 查看
Problem Description:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
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.
分析:
有序数组的查找直接使用二分查找很方便,所以旋转之后的数组要利用二分搜索的原理需要判断哪一部分仍然是有序的,然后确定在哪一部分继续搜索。具体判断方法如下:
(1)如果target==A[mid],那么mid就是我们要的结果,直接返回;
(2)如果A[begin]<=A[mid],那么说明从begin到mid一定是有序的,同样只需要判断target是否在这个范围内,如果是则把右边缘移到mid-1,否则就target在另一半,即把左边缘移到mid+1。
(3)如果A[begin]>A[mid],那么说明从mid到last一定是有序的(没有受到rotate的影响),那么我们只需要判断target是不是在mid到last之间,如果是则把左边缘移到mid+1,否则就target在另一半,即把右边缘移到mid-1。
代码如下:
class Solution {
public:
int binarysearch(int a[],int begin,int last,int target)
{
if(begin>last)
return -1;
int mid=(begin+last)/2;
if(a[mid]==target)
return mid;
if(a[begin]<=a[mid]) //这里一定要加上“=”,例子:3,1中寻找3
{
if(target>=a[begin]&&target<a[mid])
return binarysearch(a,begin,mid-1,target);
else
return binarysearch(a,mid+1,last,target);
}
else
{
if(target>a[mid]&&target<=a[last])
return binarysearch(a,mid+1,last,target);
else
return binarysearch(a,begin,mid-1,target);
}
}
int search(int A[], int n, int target) {
int flag=-1;
if(n==0)
return -1;
flag=binarysearch(A,0,n-1,target);
return flag;
}
};
下面是非递归的代码:
class Solution {
public:
int search(int A[], int n, int target) {
int begin = 0, last = n-1;
while (begin <= last)
{
int mid =(begin + last) / 2;
if (A[mid] == target)
return mid;
if (A[begin] <= A[mid])
{
if (A[begin] <= target && target < A[mid])
last = mid-1;
else
begin = mid + 1;
}
else
{
if (A[mid] < target && target <= A[last])
begin = mid + 1;
else
last = mid-1;
}
}
return -1;
}
};
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7might 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.
分析:
有序数组的查找直接使用二分查找很方便,所以旋转之后的数组要利用二分搜索的原理需要判断哪一部分仍然是有序的,然后确定在哪一部分继续搜索。具体判断方法如下:
(1)如果target==A[mid],那么mid就是我们要的结果,直接返回;
(2)如果A[begin]<=A[mid],那么说明从begin到mid一定是有序的,同样只需要判断target是否在这个范围内,如果是则把右边缘移到mid-1,否则就target在另一半,即把左边缘移到mid+1。
(3)如果A[begin]>A[mid],那么说明从mid到last一定是有序的(没有受到rotate的影响),那么我们只需要判断target是不是在mid到last之间,如果是则把左边缘移到mid+1,否则就target在另一半,即把右边缘移到mid-1。
代码如下:
class Solution {
public:
int binarysearch(int a[],int begin,int last,int target)
{
if(begin>last)
return -1;
int mid=(begin+last)/2;
if(a[mid]==target)
return mid;
if(a[begin]<=a[mid]) //这里一定要加上“=”,例子:3,1中寻找3
{
if(target>=a[begin]&&target<a[mid])
return binarysearch(a,begin,mid-1,target);
else
return binarysearch(a,mid+1,last,target);
}
else
{
if(target>a[mid]&&target<=a[last])
return binarysearch(a,mid+1,last,target);
else
return binarysearch(a,begin,mid-1,target);
}
}
int search(int A[], int n, int target) {
int flag=-1;
if(n==0)
return -1;
flag=binarysearch(A,0,n-1,target);
return flag;
}
};
下面是非递归的代码:
class Solution {
public:
int search(int A[], int n, int target) {
int begin = 0, last = n-1;
while (begin <= last)
{
int mid =(begin + last) / 2;
if (A[mid] == target)
return mid;
if (A[begin] <= A[mid])
{
if (A[begin] <= target && target < A[mid])
last = mid-1;
else
begin = mid + 1;
}
else
{
if (A[mid] < target && target <= A[last])
begin = mid + 1;
else
last = mid-1;
}
}
return -1;
}
};
相关文章推荐
- Tomcat 6.x 生成CSR
- 第三周作业——顺序查找和二分查找
- 对android的一些感想——2014-03-26
- 阶层之和
- 【每周荐书05】《新亚遗铎》(钱穆 著)
- Struts的execute方法
- word如何在图片中插入文字
- ubuntu安装curlftpfs挂载ftp目录到本地
- 去博物馆玩玩
- Android Binder简介
- Qt Creator定制
- linux下查看静态库和动态库是32位还是64位
- 高性能的数据压缩库libzling-20140324
- CKEditor插件开发
- oracle数据迁移
- 对象数组操作长方柱类
- [Android]状态栏通知Notification、NotificationManager详解
- Hibernate 关联关系
- 活用UML-软件设计高手(深圳 2014年4月26-27日)
- spring源码获得