JAVA recursive solution

  • 0
    public int integerReplacement(int n) {
            int count = 0;
            return helper((long)n, count);      
        public int helper(long n , int count){
            if(n == 1) return count;
            if(n%2==0) return helper(n/2, count+1);
            return Math.min(helper(n+1, count+1), helper(n-1, count+1));

Log in to reply

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