C++ Simple


  • 0

    You should use unsigned int to store the mid data. It may be negative

        int firstBadVersion(int n) {
            unsigned int s = 1;
            while(s < n) {
                unsigned mid = (s + n) / 2;
          
                if(isBadVersion(mid)) {
                    n = mid;
                } else {
                    s = mid + 1;
                }
            }
            
            return n;
        }
    

Log in to reply
 

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