4ms C solution easy to understand


  • 0
    C
    bool isPowerOfTwo(int n) {
        
        int i;
        bool bit1 = false;
        
        if (n < 0)
        {
            return false;
        }
        
        for (i=0; i< 31; i++)
        {
            if (n & (1<<i))
            {
                if (bit1 == true)
                {
                    bit1 = false;
                    break;
                }
                else
                {
                    bit1 = true;
                }
            }
        }
        
        return bit1;
    }

Log in to reply
 

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