class Solution {
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
public int binarySearch(int[] nums, int target) {
//write your code here
int len = nums.length;
if (len == 0) {
return -1;
}
int start = 0;
int end = len - 1;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (nums[mid] >= target) {
end = mid;
} else {
start = mid;
}
}
if (nums[start] == target) {
return start;
} else if (nums[end] == target) {
return end;
}
return -1;
}
}
Rotate Array
public int search(int[] nums, int target) {
int L = 0;
int R = nums.length - 1;
while (L <= R) {
int mid = (L + R)/2;
if (nums[mid] == target) return mid;
if (nums[L] <= nums[mid]) {
if (nums[L] <= target && target < nums[mid]) {
R = mid - 1;
} else {
L = mid + 1;
}
} else {
if (target <= nums[R] && nums[mid] < target) {
L = mid + 1;
} else {
R = mid - 1;
}
}
}
return -1;
}
No comments:
Post a Comment