7 lines concise and easy understand c++ solution


  • 0
    A
    class Solution {
    public:
        int firstBadVersion(int n) {
            int l = 0, r = n;
            while(l < r){
                int mid = l + (r - l) / 2;    //avoid overflow
                if(isBadVersion(mid)) r = mid;
                else l = mid + 1;
            }
            return l;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.