704. 二分查找 + 二分法

704. 二分查找

LeetCode_704

题目描述

代码实现

class Solution {
    public int search(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while(low <= high){
            int mid = (low + high) >> 1;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] < target){
                low =  mid + 1;
            }else{
                high = mid- 1;
            }
        }
        return -1;
    }
}
posted @ 2021-03-18 09:38  Garrett_Wale  阅读(35)  评论(0编辑  收藏  举报