Test cases
|
|
Solution 1: accepted 7ms
Binary search with wildcard while (lo + 1 < hi)
plus final conditions.
|
|
Alternative123456789101112131415161718192021public int[] searchRange(int[] nums, int target) { int lo = 0; int hi = nums.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] > target) { hi = mid - 1; } else if (nums[mid] < target) { lo = mid + 1; } else { if (nums[lo] != target) { lo++; } else if (nums[hi] != target) { hi--; } else { return new int[] {lo, hi}; } } } return new int[] {-1, -1};}