Ah,an inelegant C solution generated by another program. :-)


  • -6
    M
    bool isPowerOfTwo(int n) {
        if(n<0)
            return false;
                if(n==1||n==2||n==4||n==8||n==16||n==32||n==64||n==128||n==256||n==512||n==1024||n==2048||n==4096||n==8192||n==16384||n==32768||n==65536||n==131072||n==262144||n==524288||n==1048576||n==2097152||n==4194304||n==8388608||n==16777216||n==33554432||n==67108864||n==134217728||n==268435456||n==536870912||n== 1073741824||n==2147483648)
    return true;
    return false;
    }

  • 1
    P

    It never be a good solution when you use static refer table.


  • 0
    M

    Sorry, thumbs down not for the approach (at least it's more creative than the 100th n & (n-1) solution, which is soooo annoying) but for the horribly complicated and wrongly indented implementation and the invalid 2147483648 (that's not an int). Here's how I'd do it.

    bool isPowerOfTwo(int n) {
        return n==1||n==2||n==4||n==8||n==16||n==32||n==64||n==128||n==256||n==512||n==1024||n==2048||n==4096||n==8192||n==16384||n==32768||n==65536||n==131072||n==262144||n==524288||n==1048576||n==2097152||n==4194304||n==8388608||n==16777216||n==33554432||n==67108864||n==134217728||n==268435456||n==536870912||n==1073741824;
    }

Log in to reply
 

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