JAVA recursive solution


  • 0
    M
    public int integerReplacement(int n) {
            if(n==Integer.MAX_VALUE) return 32;
            if (n == 1) {
                return 0;
            }        
            if (n % 2 == 0) {
                return integerReplacement(n/2)+1;
            } else {
                return Math.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.