For those who has already solved Search in rotated Sorted Array, this problem can be solved similarly using code s for this problem and simply adding codes to skip the duplicates.
For Search in rotated Sorted arrays, I post solutions in C/c++/python here (C and C + + only needs one lines).
Now, based in the above codes, you can solve this problem by simply adding both lines to skip duplicates both starting from Left and right.
1 classSolution {2 Public: 3 BOOLSearch (vector<int>& Nums,inttarget) {4 intL =0, r = nums.size ()-1;5 while(L <=r) {6 while(L < r && Nums[l] = = Nums[l +1]) l++;//Skip duplicates from the left7 while(R > L && nums[r] = = Nums[r-1]) r--;//Skip duplicates from the right8 intMid = (L + r)/2;9 if(Nums[mid] = = target)return true; Ten if(Nums[mid] >target) { One if(Nums[l] <= target | | nums[mid] < NUMS[L]) R = mid-1; A ElseL = mid +1; - } - Else { the if(Nums[l] > Target | | nums[mid] >= nums[l]) L = mid +1; - ElseR = Mid-1; - } - } + return false; - } +};
[Leetcode] Search in rotated Sorted Array II