My solution in c++ with 0ms


  • 2
    W
    bool isBadVersion(int version);
    
    class Solution {
    public:
        int firstBadVersion(int n) {
            int begin = 1;
            int end = n;
            int mid;
            int firstBad = n;
            while(begin <= end)
            {
                    mid = begin + ( end - begin) / 2;
                    if(isBadVersion(mid))
                    {
                            firstBad = mid;
                            end = mid - 1;
                    }
                    else
                    {
                            begin = mid + 1;
                    }
            }
    
            return firstBad;
        }
    };

Log in to reply
 

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