美文网首页
Middle of binary search

Middle of binary search

作者: 成江 | 来源:发表于2018-03-02 04:45 被阅读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;
    

    相关文章

      网友评论

          本文标题:Middle of binary search

          本文链接:https://www.haomeiwen.com/subject/apmhuxtx.html