首页 > 代码库 > [LeetCode] Rotated Sorted Array

[LeetCode] Rotated Sorted Array

 1 public class Solution { 2     public int search(int[] A, int target) { 3         int low=0, high=A.length-1; 4          5         while (low<high) { 6             int mid=(low+high)/2; 7             if (A[mid] < A[high]) { 8                 if (target>A[mid] && target<=A[high]) low=mid+1; 9                 else high=mid;10             } else {11                 if (target>=A[low] && target<=A[mid]) high=mid;12                 else low=mid+1;13             }14         }15         16         if (A[low]==target) return low;17         18         return -1;19     }20 }

 

[LeetCode] Rotated Sorted Array