Easy and Straight forward Java Solution


  • 0
    Z
    public int integerReplacement(int n) {
    	if(n == Integer.MAX_VALUE || n == Integer.MIN_VALUE){
    		return 32;
    	}
        return helper(n, 0);
    }
    
    private int helper(int n , int count){
    	//final
    	if(n == 1){
    		return count;
    	}
    	//even
    	if(n % 2 == 0){
    		count = helper(n / 2, count + 1);
    	}
    	//odd
    	else{
    		count = Math.min(helper(n + 1, count + 1), helper(n - 1, count + 1));
    	}
    	return count;
    }

Log in to reply
 

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