Follow to "Search in rotated Sorted Array":
What if duplicates is allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given the target was in the array.
#include <vector> #include <string>using std::string;using std::vector;class Solution {Public:bool Search ( int a[], int n, int target) {if (n==0) return False;int loc = Find_max (a,n); bool flag = Binary_search (a,0,loc,target); IF (FLA g) Return Flag;flag = Binary_search (a,loc+1,n-1,target); return flag;} int Find_max (int* A, int n) {for (int i=0; i<n-1; i++) {if (A[i] > a[i+1]) return i;} return 0;} BOOL Binary_search (int* A, int low, int. high, int target) {while (Low<=high) {int mid = (Low+high)/2;if (A[mid] > target ) High--;else if (A[mid] < target) Low++;elsereturn true;} return false;}};
Leetcode--search in rotated Sorted Array II