4ms Java Solution


  • 1
    Y
    public class Solution {
        public int integerReplacement(int n) {
            int count = 0;
            long N = n;
            while (N > 1) {
                if (N % 2 != 0) {
                    if ((N + 1) / 2 % 2 == 0 && (N + 1) / 2 < (N - 1)) N += 1;
                    else N -= 1;
                } else {
                    N /= 2;
                }
                count++;
            }
            return count;
        }
    }
    

Log in to reply
 

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