Last active
April 20, 2020 03:37
-
-
Save IngeFrodo/b83d4036dafc19e31c3a34c99c807e67 to your computer and use it in GitHub Desktop.
This file contains 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
class SearchInRotatedArray { | |
public int search(int[] nums, int target) { | |
if (nums == null || nums.length == 0) { | |
return -1; | |
} | |
int left = 0; | |
int right = nums.length - 1; | |
int mid; | |
while (left <= right) { | |
mid = left + (right - left) / 2; | |
if (nums[mid] == target) { | |
return mid; | |
} else if (nums[left] <= nums[mid]) { | |
if (nums[left] <= target && nums[mid] > target) { | |
right = mid - 1; | |
} else { | |
left = mid + 1; | |
} | |
} else { | |
if (nums[mid] <= target && nums[right] >= target) { | |
left = mid + 1; | |
} else { | |
right = mid - 1; | |
} | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment