Classical Binary Search
ID: 457; easy; 经典二分查找问题
Solution 1 (Java)
Notes
The loop condition
left + 1 < right
will guarantee that we end up with two indices that are next to each other. Then, we return either of them at the end.The middle point
mid
is calculated usingleft + (right - left) / 2
instead of(left + right) / 2
in order to prevent integer overflow, which the addition may have while the subtraction will not.
Last updated