A rotating array with no repeated elements is required (the original array corresponding to the element is ordered), and the subscript of the given element in the rotating array is obtained (-1 is returned if the element does not exist ). For example, an ordered array is {, 7}, and its rotation array is {, 6, 7, 2 }. Element 6 is in the rotating array. element 2 and element 3 are not in the rotating array. The result is-1 analyzed and traversed, which can be easily done. The time complexity is O (n ), this is certainly not the optimal solution because it is obtained by rotating an ordered array. Ordered, instinctively reflected by binary search. For example, we can see that at least one of the two segments in the middle is ordered (either on the left or on the right ), then binary search can be used in the ordered range; if the ordered Range is no longer used, the second half will be searched. Copy the code int search (int A [], int n, int target) {int beg = 0; int end = n-1; while (beg <= end) {int mid = beg + (end-beg)/2; if (A [mid] = target) return mid; if (A [beg] <= A [mid]) {if (A [beg] <= target & target <A [mid]) end = mid-1; else beg = mid + 1 ;} else {if (A [mid] <target & target <= A [end]) beg = mid + 1; else end = mid-1 ;}} return-1 ;} copying code extensions involves elements that are not repeated. Now, with a slight extension, there can be duplicate elements. Complex elements. Other requirements remain unchanged. The general idea is the same as the original one, this is to compare the relationship between A [beg] And A [mid]. A [beg] <A [mid] ---- ordered A [beg]> A [mid] on the left ---- ordered A on the right [beg] = A [mid] ---- ++ beg copy the code bool search (int A [], int n, int target) {int beg = 0; int end = n-1; while (beg <= end) {int mid = beg + (end-beg)/2; if (A [mid] = target) return true; if (A [beg] <A [mid]) {if (A [beg] <= target & target <A [mid]) end = mid-1; else beg = mid + 1 ;} else if (A [beg]> A [mid]) {if (A [mid] <target & target <= A [end]) beg = mid + 1; else end = mid-1;} else ++ beg;} return false ;}