题目描述
思路
基础二分查找模板的考察。
方法一:
class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) return -1;
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (target == nums[mid]) return mid;
else if (target < nums[mid]) right = mid - 1;
else if (target > nums[mid]) left = mid + 1;
}
return -1;
}
}
标签:二分,nums,int,LeetCode704,mid,查找,LeetCode,left
From: https://www.cnblogs.com/keyongkang/p/17914658.html