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.
前阵寝室同学面百度也问到这个问题了。一般简单点会说没有重复的,如果有重复的会更复杂一些。
有两个地方要注意一下,一个是A[mid] >= A[l] ,注意等号,还有下面两个if 也是要有等号,又粗心掉了,哎。我还有救吗
class Solution { public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(A&&n>0); int l=0,r=n-1; if ( A[r]>A[l] ) return bisearch(A,l,r,target); while(l<=r) { int mid=(l+r)>>1; if ( A[mid]==target ) return mid; if ( A[mid] >= A[l] ) { if ( target>=A[l] && target <=A[mid]) return bisearch(A,l,mid,target); l=mid+1; } else { if ( target >=A[mid] && target <= A[r] ) return bisearch(A,mid,r,target); r=mid-1; } } return -1; } int bisearch(int A[],int l,int r,int tar) { while(l<=r) { int mid=(l+r)>>1; if ( A[mid] <tar ) l=mid+1; else if ( A[mid]>tar ) r=mid-1; else return mid; } return -1; } };
Search for a Range:
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
class Solution { public: vector<int> searchRange(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(A&&n>=0); int l=0,r=n-1; int left=-1,right=-1; while(l<=r) { int mid=(l+r)>>1; if ( A[mid]>target ) r=mid-1; else if ( A[mid] < target ) l=mid+1; else { left=mid; r=mid-1; } } if ( left==-1 ) return vector<int>(2,-1); else if ( left<n-1&&A[left+1]!=target) return vector<int>(2,left); l=0,r=n-1; while(l<=r) { int mid=(l+r)>>1; if ( A[mid]>target ) r=mid-1; else if ( A[mid] < target ) l=mid+1; else { right=mid; l=mid+1; } } vector<int> ans; ans.push_back(left); ans.push_back(right); return ans; } };
Search Insert Position:
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6]
, 5 → 2
[1,3,5,6]
, 2 → 1
[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 0
class Solution { public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if ( !A || n<=0) return -1; int l=0,r=n-1; while(l<=r) { int mid=(l+r)>>1; if (A[mid]<target) l=mid+1; else r=mid-1; } return l; } };
Search in Rotated Sorted Array II:
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
当无法知道是那边的时候,我用了个顺序查找,其实可以递归左右都尝试一下。只是记得把mid去掉,免得会死循环。
class Solution { public: bool search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function //assert(A&&n>=0); int l=0,r=n-1; while(l<=r) { int mid=l+((r-l)>>1); if ( A[mid]==target) return true; if (A[mid]>A[l]) { if ( A[mid]<target || A[l]>target ) l=mid+1; else return _bisearch(A,l,mid,target); } else if ( A[mid]<A[l] ) { if ( A[mid] <target && target<=A[r] ) return _bisearch(A,mid,r,target); else r=mid-1; } else { if ( A[mid]>A[r] ) l=mid+1; else { for(int i=l;i<=r;i++) if ( A[i] == target) return true; return false; } } } return false; } bool _bisearch(int A[],int l,int r,int t) { while(l<=r) { int mid=(l+r)>>1; if (A[mid]==t ) return true; else if (A[mid]<t ) l=mid+1; else r=mid-1; } return false; } };