File tree 2 files changed +43
-3
lines changed
2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Problem: 190
3
+ * Name: Reverse Bits
4
+ * Difficulty: Easy
5
+ * Topic: Bit Manipulation
6
+ * Link: https://leetcode.com/problems/reverse-bits/
7
+ */
8
+
9
+ #include < bits/stdc++.h>
10
+ using namespace std ;
11
+
12
+ // Traverse the number
13
+ // Time Complexity: O(32)
14
+ // Space Complexity: O(1)
15
+ uint32_t reverseBitsTraverse (uint32_t n) {
16
+ uint32_t result = 0 ;
17
+ for (int _ = 0 ; _ < 32 ; _++){
18
+ result <<= 1 ;
19
+ result |= n & 1 ;
20
+ n >>= 1 ;
21
+ }
22
+ return result;
23
+ }
24
+
25
+ // Specific for 32 bits
26
+ // Time Complexity: O(5)
27
+ // Space Complexity: O(1)
28
+ uint32_t reverseBitsManipulation (uint32_t n) {
29
+ // Switch the two halves of the 32 bits
30
+ n = (n >> 16 ) | (n << 16 );
31
+ // Switch the 1st/2nd half of each quarter
32
+ n = ((n & 0xff00ff00 ) >> 8 ) | ((n & 0x00ff00ff ) << 8 );
33
+ // Switch the 1st/2nd half of each eighth
34
+ n = ((n & 0xf0f0f0f0 ) >> 4 ) | ((n & 0x0f0f0f0f ) << 4 );
35
+ // Switch the 1st/2nd half of each sixteenth
36
+ n = ((n & 0xcccccccc ) >> 2 ) | ((n & 0x33333333 ) << 2 );
37
+ // Switch the 1st/2nd half of each thirty-second
38
+ n = ((n & 0xaaaaaaaa ) >> 1 ) | ((n & 0x55555555 ) << 1 );
39
+ return n;
40
+ }
Original file line number Diff line number Diff line change 16
16
17
17
### Problems Solved
18
18
19
- | Total | 38 |
19
+ | Total | 39 |
20
20
| :---:| :---:|
21
21
22
22
#### Search By Topic
27
27
| Backtracking | 0 |
28
28
| Binary Search | 2 |
29
29
| Binary Trees | 8 |
30
- | Bit Manipulation | 5 |
30
+ | Bit Manipulation | 6 |
31
31
| Dynamic Programming 1D | 1 |
32
32
| Dynamic Programming 2D | 0 |
33
33
| Graphs | 1 |
46
46
47
47
| Difficulty | Number |
48
48
| :---| ---:|
49
- | Easy | 37 |
49
+ | Easy | 38 |
50
50
| Medium | 1 |
51
51
| Hard | 0 |
52
52
You can’t perform that action at this time.
0 commit comments