JavaScript simple solution


  • 0
    D
    
    var isPowerOfTwo = function(n) {
        
        if(n < 1){
            return false;
        }
        
        // bit representation of power of 2 should contain only 1 - "1"
        return (n).toString(2).split("1").length == 2;
        
        
        // NOTE: below algo works too.. but it's TLE
        
        // if(n>2 && n%2 != 0){
        //     return false;
        // }
        
        // let b = 1;
        // while(b<n){
        //     b = b<<1;
        // }
        
        // return b==n;
        
    };
    
    
    

Log in to reply
 

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