Java backtracking simple solution


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

Log in to reply
 

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