LeetCode刷题01-数组-704.二分查找
题目:
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
示例 1:
输入:nums = [-1,0,3,5,9,12], target = 9
输出:4
解释:9 出现在 nums 中并且下标为 4
示例2:
输入:nums = [-1,0,3,5,9,12], target = 2
输出:-1
解释: 2 不存在 nums 中因此返回 -1
提示:
- 你可以假设 nums 中的所有元素是不重复的。
- n 将在 [1, 10000]之间。
- nums 的每个元素都将在 [-9999, 9999]之间。
Leetcode链接: link
代码:
class Solution {
public int search(int[] nums, int target) {
// 起始索引
int startIndex = 0;
// 终止索引
int endIndex = nums.length - 1;
// 中间索引
int halfIndex = (endIndex - startIndex) / 2;
while(startIndex <= endIndex) {
if(target < nums[halfIndex]) {
endIndex = halfIndex - 1;
}else if(target > nums[halfIndex]){
startIndex = halfIndex + 1;
}else{
return halfIndex;
}
halfIndex = startIndex + (endIndex - startIndex) / 2;
}
return -1;
}
}