快慢指针算法-二分法查询

 

    public static void main(String[] args) {
        int target = 10;
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int startI = 0;
        int endI = a.length - 1;
        while (startI <= endI) {
            int mid = (startI + endI) / 2;
            if (a[mid] == target) {
                break;
            }
            if (a[mid] < target) {
                startI = mid + 1;
            }
            if (a[mid] > target) {
                endI = mid - 1;
            }
        }
    }

 

posted @ 2020-07-14 00:34  使用D  阅读(143)  评论(0编辑  收藏  举报