• 0

    Re: A good warning to me to use start+(end-start)/2 to avoid overflow

        int firstBadVersion(int n) {     
            long start = 1;
            while (isBadVersion(start)!=true) start *= 2; 
            long a = start/2, b = start;
            long mid = a+(b-a)/2;
            while (b>a+1){
                if (isBadVersion(mid)==true) b = mid;
                else a = mid;
                mid = a+(b-a)/2;
            if (isBadVersion(a)==true) return a;
            else return a+1;        

    I use the similar trick to get the mid, but why I still get the time limit error? Anyone can help me with this?

Log in to reply

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