2-Lines Java Sol.


  • 0
    O
        public int integerReplacement(long n) {
            if (n <= 1) return (int)(1 - n);
            return 1 + ((n & 1) > 0 ? Math.min(integerReplacement(n - 1), 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.