Few lines of recursion.


  • 0
    R

    int integerReplacement(int n) {
    if(!n) return 1;
    if(n==1) return 0;
    if(!(n&1)) return integerReplacement(n/2)+1;
    else{
    if(n==INT_MAX)
    return integerReplacement(n-1);
    else
    return min(integerReplacement(n-1),integerReplacement(n+1))+1;
    }
    }


Log in to reply
 

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