少数次过
1 class Solution { 2 public: 3 int search(int A[], int n, int target) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int left, right, mid; 7 left = 0, right = n-1; 8 while (left <= right) { 9 mid = (left+right)/2; 10 if (target < A[0] && A[mid] >= A[0]) { 11 left = mid + 1; 12 continue; 13 } 14 if (target >= A[0] && A[mid] < A[0]) { 15 right = mid - 1; 16 continue; 17 } 18 if (target < A[mid]) right = mid-1; 19 else left = mid+1; 20 } 21 return target == A[(left+right)/2]? (left+right)/2 : -1; 22 } 23 };
C#
1 public class Solution { 2 public int Search(int[] nums, int target) { 3 int left = 0, right = nums.Length - 1; 4 while (left <= right) { 5 int mid = (left + right) / 2; 6 if (target < nums[0] && nums[mid] >= nums[0]) { 7 left = mid + 1; 8 continue; 9 } 10 if (target >= nums[0] && nums[mid] < nums[0]) { 11 right = mid - 1; 12 continue; 13 } 14 if (target < nums[mid]) right = mid - 1; 15 else left = mid + 1; 16 } 17 return target == nums[(left + right) / 2]? (left + right) / 2 : -1; 18 } 19 }