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.
Problem Solving Ideas:
The subject is based on Leetcode 6. Searches the search in rotated Sorted array after an ordered array is rotated, allowing duplicates to exist in an ordered array.
the difference between the previous question (which is not allowed to repeat) is that
NUMS[M] > Nums[l]: (L, M-1) monocytogenes
NUMS[M] <= Nums[l]: (m+1, R) not necessarily monocytogenes , because {1,3,1,1,1} or {1,1,1,3,1}
At this point, you can see the above conditions separately
NUMS[M] < Nums[l]: (m+1, R) must monocytogenes
Num[m] = = Nums[l]: Will l+1, re-recursive calculation ( when [L, R], will r-1)
classSolution { Public://nums array boundary is [L,r] BOOLSEARCHR ( vector<int>& Nums,intLintRintTarget) {if(R <= L)return false;intm = (l+r)/2;if(Nums[m] = = target)return true;if(Nums[l] < nums[m]) {if(Target >= nums[l] && target < nums[m])returnSEARCHR (Nums, L, M, target);Else returnSEARCHR (Nums, m+1, R, Target); }Else if(Nums[l] > Nums[m]) {if(Target > Nums[m] && target <= nums[r-1])returnSEARCHR (Nums, m+1, R, Target);Else returnSEARCHR (Nums, L, M, target); }Else{returnSEARCHR (Nums, ++l, R, Target); } }BOOLSearch vector<int>& Nums,intTarget) {returnSEARCHR (Nums,0, Nums.size (), target); }};
Leetcode 7. Searching for search in rotated Sorted array II after an ordered repeatable array rotation