Skip to content

Instantly share code, notes, and snippets.

@almaax33
Created July 16, 2020 02:03
Show Gist options
  • Save almaax33/ece2271e2147cf20110f33062f1f9b58 to your computer and use it in GitHub Desktop.
Save almaax33/ece2271e2147cf20110f33062f1f9b58 to your computer and use it in GitHub Desktop.
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] result = new int[]{-1,-1};
if(nums.length == 0 || nums[0] > target) return result;
int start = 0, end = nums.length-1, midPoint = 0;
boolean found = false;
while(start<=end && !found){
midPoint = Math.round((start + end) / 2);
if(nums[midPoint] == target){
start = midPoint;
end = midPoint;
found = true;
} else if(nums[midPoint] < target){
start = midPoint + 1;
} else if(nums[midPoint] > target){
end = midPoint - 1;
}
}
if(start == end && found)
while(result[0] == -1 || result[1] == -1){
if(start >= 0 && nums[start] == target){
start--;
} else if(result[0] == -1) {
result[0] = start+1;
}
if(end < nums.length && nums[end] == target){
end++;
} else if(result[1] == -1) {
result[1] = end-1;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment