Share my 12ms recursive C++ code


  • 0
    I

    Easy recursive code.

    int integerReplacement(int n) {
        long long int ln = (long long int)n;
        return integerReplacementLong(ln);
        
    }
    int integerReplacementLong(long long int ln)
    {
        if(ln<=1)
        {
            return 0;
        }
        
        if(!(ln%2))
        {
            return 1+integerReplacementLong(ln/2);
        }
        else
        {
            return 1+min(integerReplacementLong(ln+1),integerReplacementLong(ln-1));
        }
    }

Log in to reply
 

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