auto commit

This commit is contained in:
CyC2018 2018-06-11 09:33:00 +08:00
parent 582ca62941
commit 4cb2e2f971

View File

@ -994,11 +994,12 @@ public int firstBadVersion(int n) {
int l = 1, h = n; int l = 1, h = n;
while (l < h) { while (l < h) {
int mid = l + (h - l) / 2; int mid = l + (h - l) / 2;
if (isBadVersion(mid)) if (isBadVersion(mid)) {
h = mid; h = mid;
else } else {
l = mid + 1; l = mid + 1;
} }
}
return l; return l;
} }
``` ```
@ -1017,11 +1018,12 @@ public int findMin(int[] nums) {
int l = 0, h = nums.length - 1; int l = 0, h = nums.length - 1;
while (l < h) { while (l < h) {
int m = l + (h - l) / 2; int m = l + (h - l) / 2;
if (nums[m] <= nums[h]) if (nums[m] <= nums[h]) {
h = m; h = m;
else } else {
l = m + 1; l = m + 1;
} }
}
return nums[l]; return nums[l];
} }
``` ```
@ -1042,21 +1044,23 @@ Output: [-1,-1]
public int[] searchRange(int[] nums, int target) { public int[] searchRange(int[] nums, int target) {
int first = binarySearch(nums, target); int first = binarySearch(nums, target);
int last = binarySearch(nums, target + 1) - 1; int last = binarySearch(nums, target + 1) - 1;
if (first == nums.length || nums[first] != target) if (first == nums.length || nums[first] != target) {
return new int[]{-1, -1}; return new int[]{-1, -1};
else } else {
return new int[]{first, Math.max(first, last)}; return new int[]{first, Math.max(first, last)};
}
} }
private int binarySearch(int[] nums, int target) { private int binarySearch(int[] nums, int target) {
int l = 0, h = nums.length; // 注意 h 的初始值 int l = 0, h = nums.length; // 注意 h 的初始值
while (l < h) { while (l < h) {
int m = l + (h - l) / 2; int m = l + (h - l) / 2;
if (nums[m] >= target) if (nums[m] >= target) {
h = m; h = m;
else } else {
l = m + 1; l = m + 1;
} }
}
return l; return l;
} }
``` ```