Created
March 4, 2014 06:49
-
-
Save rayjcwu/9341506 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public boolean search(int [] A, int val) { | |
| int low = 0; | |
| int high = A.length - 1; // inclusive | |
| while (low <= high) { | |
| int mid = (low + high) / 2; | |
| if (A[mid] == val) { | |
| return true; | |
| } else if (A[low] < A[mid]) { | |
| if (A[low] <= val && val < A[mid]) { | |
| high = mid - 1; | |
| } else { | |
| low = mid + 1; | |
| } | |
| } else if (A[mid] < A[high]) { | |
| if (val <= A[high] && A[mid] < val) { | |
| low = mid + 1; | |
| } else { | |
| high = mid - 1; | |
| } | |
| } else if (A[low] == A[mid]) { | |
| low++; | |
| } else if (A[mid] == A[high]) { | |
| high--; | |
| } | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment