LeetCode 算法题解与代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int search(int[] nums, int target) {
int left = -1, right = nums.length;
while (left + 1 != right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid;
} else {
right = mid;
}
}
if (right == nums.length || nums[right] != target) {
return -1;
}
return right;
}
}

Comments