【java】704. Binary Search

问题原文https://leetcode-cn.com/problems/binary-search/description/

public int search(int[] nums, int target) {
        int i = 0;
        int j = nums.length-1;
        if(target<nums[i] || target>nums[j]) return -1;
        while(i<j) {
        	int mid = i+(j-i)/2;
        	if(nums[mid] == target) return mid; 
        	else if(nums[mid] > target) {
        		j = mid-1;
        	}else {
        		i = mid+1;
        	}
        	
        }
        return -1;
    }

猜你喜欢

转载自blog.csdn.net/amber804105/article/details/81168762