4 Lines C++ recursion solution


  • 0
    int integerReplacement(int n) {
            if(n==1) return 0;
            if(n==INT_MAX) return 32;
            if(n%2 == 0) return 1 + integerReplacement(n/2);
            return 1 + min(integerReplacement(n-1), integerReplacement(n+1));
        }
    

Log in to reply
 

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