标签:
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.
Array Binary Search
class Solution { public: bool search(int A[], int n, int target) { if(n==1) return A[0]==target?true:false; return help(A,0,n-1,target); } int help(int* & A,int l,int r,int &target) { if(target<A[l]&&target>A[r]) return false; if(target==A[l]) return true; if(target==A[r]) return true; if(r-l==1) return false; int m = (l+r) /2; if(target==A[m]) return true; if(A[l]<A[m]){ if(A[l]<target&&target<A[m]) return help(A,l,m,target); return help(A,m,r,target); } else if(A[l]>A[m]){ if(A[m]<target&&target<A[r]) return help(A,m,r,target); return help(A,l,m,target); } else{ return help(A,l,m,target)||help(A,m,r,target); } } };
[LeetCode] Search in Rotated Sorted Array II 二分搜索
标签:
原文地址:http://www.cnblogs.com/Azhu/p/4240540.html