-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (86 loc) · 2.49 KB
/
main.cpp
File metadata and controls
97 lines (86 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Source: https://leetcode.com/problems/binary-gap
// Title: Binary Gap
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a positive integer `n`, find and return the **longest distance** between any two **adjacent** `1`'s in the binary representation of `n`. If there are no two adjacent `1`'s, return `0`.
//
// Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `1`'s is the absolute difference between their bit positions. For example, the two `1`'s in `"1001"` have a distance of 3.
//
// **Example 1:**
//
// ```
// Input: n = 22
// Output: 2
// Explanation: 22 in binary is "10110".
// The first adjacent pair of 1's is "10110" with a distance of 2.
// The second adjacent pair of 1's is "10110" with a distance of 1.
// The answer is the largest of these two distances, which is 2.
// Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
// ```
//
// **Example 2:**
//
// ```
// Input: n = 8
// Output: 0
// Explanation: 8 in binary is "1000".
// There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
// ```
//
// **Example 3:**
//
// ```
// Input: n = 5
// Output: 2
// Explanation: 5 in binary is "101".
// ```
//
// **Constraints:**
//
// - `1 <= n <= 10^9`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <bit>
using namespace std;
// Loop through all bits
class Solution {
public:
int binaryGap(unsigned int n) {
int maxGap = 0;
// Loop through all bits
int prevBit = -1, bit = 0;
while (n > 0) {
if (n & 1u) {
if (prevBit >= 0) {
maxGap = max(maxGap, bit - prevBit);
}
prevBit = bit;
}
n >>= 1;
++bit;
}
return maxGap;
}
};
// Bit trick
class Solution2 {
public:
int binaryGap(unsigned int n) {
int maxGap = 0;
// Edge case
if (n == 0) return 0;
// Find first bit
int prevIdx = countr_zero(n); // find lowbit
n &= (n - 1); // remove lowbit
// Find next bit
while (n > 0) {
int currIdx = countr_zero(n); // find lowbit
n &= (n - 1); // remove lowbit
maxGap = max(maxGap, currIdx - prevIdx);
prevIdx = currIdx;
}
return maxGap;
}
};