给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。

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
        int start = 0;
        int end = nums.length - 1;

        while(start + 1 < end) {
            int middle = start + (end - start) / 2;
            if(nums[middle] >= target) {
                end = middle;
            } else {
                start = middle;
            }
        }

        if(nums[start] == target) return start;
        if(nums[end] == target) return end;

        return -1;
    }
}

results matching ""

    No results matching ""