704. 二分查找

https://leetcode-cn.com/problems/binary-search/

class Solution {
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;
        while(low <= high){
            int mid = (low+high)/2;
            if ( nums[mid] > target ){
                high = mid - 1;
            }

            if( nums[mid] < target ){
                low = mid + 1;
            }  

            if( nums[mid] == target ){
                return mid;
            }
        }
        return -1;
    }
}

  

原文地址:https://www.cnblogs.com/longlyseul/p/15305121.html