# LeetCode 231.Power of Two is simple

Posted by sendoh07 on Tue, 28 Sep 2021 18:04:40 +0200

This article belongs to "Conquer LeetCode"One of the series, which began officially in 2021/08/12. Because some of the titles on LeetCode are locked, this series will last at least until the day all the unlocked titles are brushed off. Because LeetCode is still creating new titles, the end date of this series may be forever. In this series of brushing titles, I will not only explain a variety of ideas and their optimization, but will also use a variety of optionsThe programming language implements the puzzle and summarizes the corresponding algorithm templates when it comes to general solutions.

To facilitate running debugging on PC s and sharing code files, I also set up a warehouse: https://github.com/memcpy0/LeetCode-Conquest In this warehouse, you can not only see the LeetCode topic links, problem code, problem article links, similar topic summaries, general solution summaries, etc., but also see important information such as the frequency of the original topic and related enterprises. If you have other preferred solutions, you can also share them with others.

As the content of this series of articles may change at any time, you are welcome to follow and collect them Conquer LeetCode Series Articles Directory One article for memo.

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:

```Input: n = 1
Output: true
Explanation: 2^0 = 1
```

Example 2:

```Input: n = 16
Output: true
Explanation: 2^4 = 16
```

Example 3:

```Input: n = 3
Output: false
```

Example 4:

```Input: n = 4
Output: true
```

Example 5:

```Input: n = 5
Output: false
```

Constraints:

• -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?

Title: Give you an integer n, you can judge if it is the power of 2. If it is, return true; otherwise, return false. Advanced does not use loops or recursion to complete this topic.

This question is similar to 326. Power of Three and 342. Power of Four Especially for a power like 4.

### Solution 1 Iteration+Trial

The simple solution is to keep adding n n n pairs 2 2 2 Trial until n n n is no longer associated with 2 2 2 is multiplied, then judge n n Is n equal to 2 0 = 1 2^0 = 1 20 = 1. Note that nonpositive integers are not 2 2 The time complexity of the algorithm is O ( log ⁡ n ) O(\log n) O(logn), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
while (n % 2 == 0) n /= 2;
return n == 1;
}
};
//Execution time: 0 ms, beating 100.00% of all C++ submissions
//Memory consumption: 5.6 MB, beating 98.27% of all C++ submissions
```

I am here Power of 4 The sx method in can also be used here. n n n to a floating point number and divide by 2 2 2, and finally decide if it's equal to 1.0 1.0 1.0 (not recommended):

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
double tn = n;
while (tn > 1.0) tn /= 2;
return tn == 1.0;
}
};
//Execution time: 4 ms, beating 47.41% of all C++ submissions
//Memory consumption: 5.8 MB, beating 70.78% of all C++ submissions
```

### Solution 2-bit operation (1 moving left continuously)

Keep going 1 1 1 Move Left 1 1 1-bit and n n n Compare until greater than or equal to n n Until n. This saves some constant time compared to using division or even floating-point division. To avoid overflow errors, use the unsigned integer type uint32_t (be careful to exclude non-positive integers, especially negative integers) or something like Power of 4 Use long long long as well. The time complexity of the algorithm is O ( log ⁡ n ) O(\log n) O(logn), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
uint32_t x = 1;
while (x < n) x <<= 1;
return x == n;
}
};
//Execution time: 4 ms, beating 47.41% of all C++ submissions
//Memory consumption: 5.8 MB, beating 66.92% of all C++ submissions
```

### Solution 3 Math library function (not recommended)

Use the math library functions log(), pow(), but here you need to determine if n is a positive integer and handle the error (which may be the test sample ratio) 326. Power of Three Much less, just over a thousand) and then passed:

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
int k = log(n) / log(2);
return pow(2, k) == n;
}
};
//Execution time: 4 ms, beating 47.41% of all C++ submissions
//Memory consumption: 5.9 MB, beating 38.62% of all C++ submissions
```

### Solution 4 Tables

One of the easiest ways to think of "do not use loops/recursions" is to do table preprocessing. The time complexity of the algorithm is O ( 1 ) O(1) O(1), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
int ans = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
for (int i = 0; i < 32; ++i) if (n == ans[i]) return true;
return false;
}
};
//Execution time: 0 ms, beating 100.00% of all C++ submissions
//Memory consumption: 5.9 MB, beating 26.88% of all C++ submissions
```

### Solution 5 Mathematics (Multiple/Approximate)

n n The data type of n is int, as shown by the table, which is the largest in the int range 2 2 The second power is 1073741824 1073741824 1073741824. If n n n is 2 2 The power of 2 must be satisfied n ∗ 2 k = 1073741824 n * 2^k = 1073741824 n_2k=1073741824, that is n n n and 1073741824 1073741824 There is a multiple relationship between 1073741824. So you just need to decide n n n is a positive integer and is 1073741824 1073741824 The time complexity of the algorithm is O ( 1 ) O(1) O(1), spatial complexity is O ( 1 ) O(1) O(1) .

It is important to note that this is not a quick judgement x x General practice for the power of x if and only if x x x is a prime number, so it cannot be generalized to 342. Power of Four A simple example is, 64 64 64 is 4 4 The power of 4 must also be some of the largest 4 4 Power of 4 z z The approximate number of z, however 32 32 32 is not 4 4 The power of 4, but it must be 64 64 64, thus z z The approximate number of z.

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && 1073741824 % n == 0;
}
};
//Execution time: 0 ms, beating 100.00% of all C++ submissions
//Memory consumption: 5.8 MB, beating 58.76% of all C++ submissions
```

### Solution 6-bit operation skills

With bitwise manipulation, we can easily solve this problem. In theory, in binary form 2 2 Sum of powers of 2 4 4 Like the power of 4, there is only one binary bit 1 1 1, so you can use n > 0 to judge positive numbers, use!(n &(n - 1)) Judges that only one of the binary forms appears 1 1 1. The time complexity of the algorithm is O ( 1 ) O(1) O(1), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n & (n - 1));
}
};
//Execution time: 0 ms, beating 100.00% of all C++ submissions
//Memory consumption: 5.9 MB, beating 26.88% of all C++ submissions
```
1. Power of 2
https://leetcode-cn.com/problems/power-of-two/

Topics: Algorithm leetcode Math