Leetcode 231 - Power of 2

Note:

  • Using iteration is weak.
  • LOWBIT! - To get the lowest 1 in a number.
  • Power of 2 only has one 1. So n & -n === n.

Question:

Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

Example:

1
2
3
Input: n = 1
Output: true
Explanation: 20 = 1

Code:

Math

1
2
3
4
5
6
7
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
return n > 0 && (n & -n) === n;
};

Iteration

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
if (n <= 0) return false;
while (n !== 1) {
if (n % 2 !== 0) return false;
n = n / 2;
}
return true;
};