278. 第一个错误的版本

题目

代码

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int low=1,high=n;
        while(low<=high)
        {
            //这里可能导致溢出
            int mid=low/2+high/2+(low%2+high%2)/2;
            cout<<mid<<endl;
            if(isBadVersion(mid))
            {
               if(isBadVersion(mid-1)==false)
                    return mid;
                else
                {
                    high=mid-1;
                    continue;
                }
            }
            else
            {
                low=mid+1;
                continue;
            }
        }
        
        
    }
};

思路

利用二分查找的思想。

https://github.com/li-zheng-hao
原文地址:https://www.cnblogs.com/lizhenghao126/p/11053622.html