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.
1 class Solution { 2 public: 3 bool search(vector<int>& nums, int target) { 4 int a=0,b=nums.size()-1; 5 while(a<=b) 6 { 7 int m=a+(b-a)/2; 8 if(nums[m]==target) 9 return true; 10 if(nums[a]<target&&target<nums[m]) 11 b=m-1; 12 else if(nums[m]<target&&target<nums[b]) 13 a=m+1; 14 else 15 { 16 if(nums[a]==target) 17 return true; 18 else 19 a++; 20 if(nums[b]==target) 21 return true; 22 else 23 b--; 24 } 25 } 26 return false; 27 } 28 };