704. 二分查找

题目:

思路:

【1】常规二分代码

代码展示:

//时间0 ms 击败 100%
//内存43.1 MB 击败 68.49%
class Solution {
    public int search(int[] nums, int target) {
        int l = 0 , r = nums.length;
        while (l < r){
            int mid = (r-l)/2 + l;
            if (nums[mid] == target){
                return mid;
            }else if (nums[mid] > target){
                r = mid;
            }else {
                l = mid+1;
            }
        }
        return -1;
    }
}
posted @ 2023-08-15 17:47  忧愁的chafry  阅读(5)  评论(0编辑  收藏  举报