Recursive C++ Solution


  • 0
    S
    // Forward declaration of isBadVersion API.
    bool isBadVersion(int version);
    
    class Solution {
    public:
        int a = 1;
        int firstBadVersion(int n) 
        {
            int midVersion = a + ( n - a)/2;
            if ( isBadVersion( midVersion ) )
            {
                while ( midVersion >= a )
                {
                    if ( !isBadVersion( midVersion ) )
                    {
                        break;
                    }
                    
                    midVersion--;
                }
                
                return midVersion + 1;
            }
            else
            {
                a = midVersion + 1;
                return firstBadVersion(n);
            }
        }
    };
    

Log in to reply
 

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