题目 |
题目地址:传送门(点击此处)
题解 |
很基础的二分查找算法,不做过多的解释了
class Solution {
public int search(int[] nums, int target) {
int res;
int left = 0;
int right = nums.length - 1;
while (left <= right) {
res = left + (right - left) / 2;
if (target == nums[res]) return res;
if (target > nums[res]) left = res + 1;
else right = res - 1;
}
return -1;
}
}