无法播放?请 点击这里 跳转到Youtube
切换视频源:

题目链接: https://leetcode.com/problems/binary-search/

GitHub答案链接: https://github.com/turingplanet/leetcode-answers

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