← Home

231. Power of Two

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
231

This is one of those problems where the clean idea matters more than the amount of code. For 231. Power of Two, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isPowerOfTwo.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 37.98% of C++ online submissions for Power of Two.
02//Memory Usage: 6.1 MB, less than 16.52% of C++ online submissions for Power of Two.
03class Solution {
04public:
05    bool isPowerOfTwo(int n) {
06        if(n == 0) return false;
07        while(n % 2 == 0){
08            n /= 2;
09        }
10        return n == 1;
11    }
12};
13
14//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Power of Two.
15//Memory Usage: 7.9 MB, less than 97.02% of C++ online submissions for Power of Two.
16class Solution {
17public:
18    bool isPowerOfTwo(int n) {
19        //negative number has a factor -1, 
20        //so it's not a power of two
21        if(n <= 0) return false;
22        while(n > 0){
23            //if there is a 1-bit in n,
24            //then it should be the first bit
25            //ex: 16 = 1000, 1 = 1, 17 = 1001
26            if(n & 1 == 1 && n != 1) return false;
27            n >>= 1;
28        }
29        return true;
30    }
31};
32
33//https://leetcode.com/problems/power-of-two/discuss/63974/Using-nand(n-1)-trick
34//Runtime: 8 ms, faster than 44.60% of C++ online submissions for Power of Two.
35//Memory Usage: 7.9 MB, less than 98.72% of C++ online submissions for Power of Two.
36class Solution {
37public:
38    bool isPowerOfTwo(int n) {
39        if(n <= 0) return false;
40        //the power of 2 only has one bit-1,
41        // n&(n-1) will make the last bit-1 be zero,
42        // and if n&(n-1) is 0, 
43        // it means n has only one bit-1, 
44        // so it should be a power of 2
45        return !(n&(n-1));
46    }
47};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.