This problem looks busy at first, but the accepted solution is built around one steady invariant. For 693. Binary Number with Alternating Bits, the solution in this repository is mainly a bit manipulation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: bit manipulation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are hasAlternatingBits.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- 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(1).
- Space: O(1), or alternatively O(w).
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
03
04Example 1:
05Input: 5
06Output: True
07Explanation:
08The binary representation of 5 is: 101
09Example 2:
10Input: 7
11Output: False
12Explanation:
13The binary representation of 7 is: 111.
14Example 3:
15Input: 11
16Output: False
17Explanation:
18The binary representation of 11 is: 1011.
19Example 4:
20Input: 10
21Output: True
22Explanation:
23The binary representation of 10 is: 1010.
24**/
25
26//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Binary Number with Alternating Bits.
27//Memory Usage: 9.5 MB, less than 7.32% of C++ online submissions for Binary Number with Alternating Bits.
28
29/**
30Approach #1: Convert to String [Accepted]
31Intuition and Algorithm
32
33Let's convert the given number into a string of binary digits.
34Then, we should simply check that no two adjacent digits are the same.
35**/
36
37/**
38Complexity Analysis
39
40Time Complexity: O(1).
41For arbitrary inputs, we do O(w) work, where w is the number of bits in n.
42However, w≤32.
43
44Space complexity: O(1), or alternatively O(w).
45**/
46
47class Solution {
48public:
49 bool hasAlternatingBits(int n) {
50 int l = ceil(log2(n));
51 bitset<40> b(n);
52 int last = b[0];
53 for(int i = 1; i < l; i++){
54 if(b[i]==last){
55 return false;
56 }else{
57 last = b[i];
58 }
59 }
60 return true;
61 }
62};
63
64/**
65Approach #2: Divide By Two [Accepted]
66Intuition and Algorithm
67
68We can get the last bit and the rest of the bits via n % 2 and n // 2 operations.
69Let's remember cur, the last bit of n.
70If the last bit ever equals the last bit of the remaining,
71then two adjacent bits have the same value, and the answer is False.
72Otherwise, the answer is True.
73
74Also note that instead of n % 2 and n // 2, we could have used operators n & 1 and n >>= 1 instead.
75**/
76
77/**
78Complexity Analysis
79
80Time Complexity: O(1).
81For arbitrary inputs, we do O(w) work, where w is the number of bits in n.
82However, w≤32.
83
84Space complexity: O(1).
85**/
86
87//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Binary Number with Alternating Bits.
88//Memory Usage: 9.3 MB, less than 15.85% of C++ online submissions for Binary Number with Alternating Bits.
89
90/**
91class Solution {
92public:
93 bool hasAlternatingBits(int n) {
94 int cur = n%2;
95 n/=2;
96 while(n > 0){
97 if(cur==n%2) return false;
98 cur = n%2;
99 n/=2;
100 }
101 return true;
102 }
103};
104**/
Cost