C++ 0ms bit-manulation


  • 0
    Y
    class Solution {
    public:
      int integerReplacement(int n) {
        int res = 0;
        unsigned m = n;
        while (m > 3) {
          if (!(m & 1))
            m >>= 1;
          else if (m & 2)
            m++;
          else
            m--;
          res++;
        }
        return res + m - 1;
      }
    };
    

Log in to reply
 

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