Recursive solution


  • 0
    R

    A normal recursive solution handling the conditions given

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