Middle of binary search

2018-03-02  本文已影响4人  成江

When doing binary search, we need to calculate the middle pointer.
Be careful about the overflow. Because it is leftStart + rightEnd might overflow.

int mid = leftStart + (rightEnd - leftStart) / 2;

The following is not the same.

int mid = (leftStart + rightEnd) / 2;
上一篇下一篇

猜你喜欢

热点阅读