JavaScript regex solution

  • 3
     * @param {number} num
     * @return {boolean}
    var isPowerOfFour = function(num) {
      return /^1(00)*$/.test(num.toString(2));  
    • JavaScript does conversion to string representation of a binary number with toString(2). The number to the left requires parenthesis when using a literal number (which isn't the case here).
    • The string must have an even number of zeros to the right of a 1 in the first position.

Log in to reply

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