码迷,mamicode.com
首页 > 其他好文 > 详细

278. First Bad Version

时间:2018-04-06 20:18:31      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:logs   最大值   markdown   clear   first   方法   solution   control   ble   

原题链接:https://leetcode.com/problems/first-bad-version/description/``
实现如下:

/**
 * Created by clearbug on 2018/4/6.
 */
public class VersionControlSolution extends VersionControl {

    public int firstBadVersion(int n) {
        int start = 1;
        int end = n;
        while (start <= end) {
//            int medium = (start + end) / 2; // 不要使用这种方法,当 start 和 end 都接近 int 类型最大值时会导致溢出,以致 medium 可能成为负数
            int medium = start + (end - start) / 2;
            if (isBadVersion(medium)) {
                end = medium - 1;
            } else {
                start = medium + 1;
            }
        }
        return start;
    }
    
}

278. First Bad Version

标签:logs   最大值   markdown   clear   first   方法   solution   control   ble   

原文地址:https://www.cnblogs.com/optor/p/8728560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!