JavaScript Solution


  • 0
    S
    var isPowerOfTwo = function(n) {
        while(n>=2){
        	n = n/2;
        }
        return n===1?true:false;
    };
    
    
    
    var isPowerOfTwo = function(n) {
        n = n.toString(2);
        var len = n.length-1;
        var re = new RegExp("0"+"{"+len+"}")
        if(n[0]==="1"&&re.test(n.slice(1))){
          return true;
        }
        return false;
    };
    

Log in to reply
 

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