- 二分查找
中文
English
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。
样例
样例 1:
输入:[1,4,4,5,7,7,8,9,9,10],1
输出: 0
样例解释:
第一次出现在第0个位置。
样例 2:
输入: [1, 2, 3, 3, 4, 5, 10],3
输出: 2
样例解释:
第一次出现在第2个位置
样例 3:
输入: [1, 2, 3, 3, 4, 5, 10],6
输出: -1
样例解释:
没有出现过6, 返回-1
挑战
如果数组中的整数个数超过了2^32,你的算法是否会出错?
public class Solution {
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
public int binarySearch(int[] nums, int target) {
// write your code here
return Search(nums,target,0,nums.length);
}
public int Search(int []nums,int target,int first,int last)
{
if(first==last&&nums[0]==target) return first;
else if(first==last&&nums[0]!=target) return -1;
else
{
int n=(first+last)/2;
if(nums[n]==target)
{
if(Search(nums,target,first,n)==-1) return n;
else return Search(nums,target,first,n);
}
else if(nums[n]>target) return Search(nums,target,first,n);
else return Search(nums,target,n+1,last);
// return -1;
}
}
}