ACM 1 array # include <stdio.h> #include "sort.h" int arrdump (int * A, int n) {int i = 0;printf ("array:["); for (i=0; i<n; i++) {printf ("%d", A[i]);} printf ("]\n"); return 0;} /* Remove repeating elements in an ordered array time O (n) space O (1) */int removesorteddup (int * A, int n, int * len) {int i = 0, index = 0;for (i=1; i<n; i++) {i F (a[i]! = A[index])//no Duplicate{a[++index] = A[i];//resort}}*len = Index+1;return 0;} /* Allow up to two repetition times O (n) space O (1) */int removesortedduptwice (int * A, int n, int * len) {int i = 0, index = 0, sum = 1;for (i=1; i< ; n; i++) {if (A[i]! = A[index])//no Duplicate{a[++index] = A[i];//resortsum = 1;} else if (sum <2) {A[++index] = a[i];sum + +;}} *len = Index+1;return 0;} /* Online code */int removeduplicates (int a[], int n) {int index = 2, i=2; if (n <= 2) return n; for (i = 2; i < n; i++) {if (A[i]! = A[index-2])//Window index-2, index-1 a[index++] = a[i];// After Operation A[index] is always different from previous elements} return index; }/* binary Find */int binarysearch (int a[], int len, int target) {int left = 0, right = len-1, Mid = 0;while (left <= right) {mid = (left+right)/2;if (target = = A[mid]) {return mi D;} else if (target < A[mid]) {right = Mid-1;} Else{left = mid + 1;}} return-1;} /* The ordered array is broken from the middle, the second and second parts of the search in rotated Sorted array difficulty lies in determining the dichotomy boundary without using direct lookup, using binary lookup time O (log n) space O (1) */int rotatesearch (int a[], int len, int target) {int left = 0, right = len-1, Mid = 0;while (left <= right) {mid = (left+right)/2;if (a[mid] = = TA Rget) return mid;if (A[mid] < A[left])//with A[mid] > A[right], because at this time a[left]>a[right]{if (Target>a[mid] & & Target<=a[right]) left = mid + 1;elseright = mid-1;} else//if (A[mid] >= A[left]) {if (Target>=a[left] && Target<a[mid])//with Target>=a[left] && Target<=a[mid]right = Mid-1;elseleft = mid + 1; }}return-1;} Ibid., called before the binary lookup implements//int rotatesearch (int a[], int len, int target)//{//int left = 0, right = len-1, Mid = 0;//while (left &L T right)//{//mid = (left+right)/2;//if (A[mid] < A[left])//with A[mid] > A[right], because at this time A[left]>a[right]//{//right = Mid;//}//else if (A[mid] > a[right])//{//left = mid;//}//if (left = = right-1)//{//mid = a[left]>a[right]? left:right;//break;//pivot = mid//}//}//printf ("Pivot index:%d\n", mid);//if (A[mid] = target)//{//return mid;//}//else if (Target > A[0])//{//return BinarySearch (A, mid+1, target);//}//else//{ Return (mid+1 + binarysearch (&a[mid+1], Len-mid, target)),//}//}/*search in rotated Sorted array allows repeating elements, Find out if the target exists in the array special case: Array ordered but discontinuous */int rotatesearchdup (int a[], int len, int target) {int left = 0, right = len-1, mid = 0, TM p = -1;while (left <= right) {mid = (left+right)/2;if (A[mid] = = target) return Mid;else if (right-left <= 1) {retur n-1;} if (A[mid] > A[left])//with A[mid] > A[right], because at this time a[left]>a[right]{if (Target>=a[left] && target<a[ MID])//with Target>=a[left] && target<=a[mid]right = Mid-1;elseleft = mid + 1; }else if (A[mid] < A[right]) {if (Target>a[mid] && taRget<=a[right]) left = mid + 1;elseright = mid-1;} else if (a[mid] = = A[left])//special situation{if (Target > A[mid]) {tmp = Rotatesearchdup (&a[left], mid-left+1, tar get); if (tmp! =-1) return left+tmp;tmp = Rotatesearchdup (&a[mid+1], Right-mid, target), if (tmp! =-1) return MID+1+TM P;} else left = mid+1;} else if (a[mid] = = A[right]) {if (Target > A[mid]) {tmp = Rotatesearchdup (&a[mid], right-mid+1, target); if (tmp! =-1 return mid+tmp;tmp = Rotatesearchdup (&a[left], mid-left, target); if (tmp! =-1) return left+tmp;} else right = Mid-1;}} return-1;}
An ACM web site to see the topic, the original question is English, and today a back again can not find ...
Some questions about array lookups