Straightforward java solution NO MATH


  • 0
    Y
    class Solution {
        public int integerReplacement(int n) {
            return (int) helper((long)n);
        }
        private long helper(long n){
            if(n<=1) return 0;
            if(n%2==0) return helper(n/2)+1;
            else{
                return Math.min(helper(n+1),helper(n-1))+1;
            }        
        }
    }
    

Log in to reply
 

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