leetcode 278. First Bad Version

    int firstBadVersion(int n) {
        // long start = 1;
        // long end = n;
        int start = 1;
        int end = n;
        
        int last = -1;
        while (start <= end) {
            // int mid = (start + end) / 2;  // if use this way, var 'start' and 'end' must be long type.
            int mid = start + (end - start) / 2;
            if (isBadVersion(mid)) {
                last = mid;
                end = mid - 1;
            } 
            else
                start = mid + 1;
        }
        if (last != -1)
            return last;
        return n;
    }

 

posted on 2018-02-08 11:55  willaty  阅读(95)  评论(0编辑  收藏  举报

导航