[传统算法] 二分查找 Binary Search C++实现

    int search(vector& nums, int target) {
        int l = 0, r = nums.size()-1;
        while(l <= r){
            int m = (l+r) / 2;
            if(nums[m] == target) return m;
            if(nums[m] < target)  l = m+1;
            else                  r = m-1;
        }
        return -1;
    }

代码通过了leetcode样例的测试,写在这里备查 

你可能感兴趣的:(传统算法)