Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

##Binary Search

####正常用法

  • 需要sorted
  • 一般不带duplicate,有duplicate就有变化

####log(n)

  • k numbers ---> k/2 numbers

####Basic Model

int end = nums.length - 1;
int start = 0;

while (end > start+1) {
    int mid = start + (end - start) / 2;
    if (nums[mid] == target) {
        end = mid;
    } else if (nums[mid] > target) {
        end = mid;
    } else {
        start = mid;
    }
}

if (nums[end] == target) {
    return end;
} else if(nums[start] == target) {
    return start;
}
<script async defer id="github-bjs" src="proxy.php?url=https%3A%2F%2Fwww.github.com%2F%3Ca+href%3D"https://buttons.github.io/buttons.js"></script>" rel="nofollow">https://buttons.github.io/buttons.js"></script>