Skip to content

Commit b672d8f

Browse files
committed
一刷868
1 parent 454d32b commit b672d8f

7 files changed

Lines changed: 100 additions & 79 deletions

File tree

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6101,12 +6101,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
61016101
|Easy
61026102
|
61036103

6104-
//|{counter:codes}
6105-
//|{leetcode_base_url}/binary-gap/[868. Binary Gap^]
6106-
//|{source_base_url}/_0868_BinaryGap.java[Java]
6107-
//|{doc_base_url}/0868-binary-gap.adoc[题解]
6108-
//|Easy
6109-
//|
6104+
|{counter:codes}
6105+
|{leetcode_base_url}/binary-gap/[868. Binary Gap^]
6106+
|{source_base_url}/_0868_BinaryGap.java[Java]
6107+
|{doc_base_url}/0868-binary-gap.adoc[题解]
6108+
|Easy
6109+
|
61106110

61116111
|{counter:codes}
61126112
|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]

docs/0000-28-math.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
== 典型技巧
77

8+
=== 位运算
9+
10+
image::images/binar-fundamentals.png[{image_attr}]
11+
812
=== 最大公约数
913

1014
stem:[gcd(max, min)=gcd(min,max mod min)]

docs/0868-binary-gap.adoc

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,75 @@
11
[#0868-binary-gap]
2-
= 868. Binary Gap
2+
= 868. 二进制间距
33

4-
{leetcode}/problems/binary-gap/[LeetCode - Binary Gap^]
4+
https://leetcode.cn/problems/binary-gap/[LeetCode - 868. 二进制间距^]
55

6-
Given a positive integer `N`, find and return the longest distance between two consecutive 1's in the binary representation of `N`.
6+
给定一个正整数 `n`,找到并返回 `n` 的二进制表示中两个 *相邻* 1 之间的 *最长距离* 。如果不存在两个相邻的 1,返回 `0`
77

8-
If there aren't two consecutive 1's, return <font face="monospace">0.
8+
如果只有 `0` 将两个 `1` 分隔开(可能不存在 `0` ),则认为这两个 1 彼此 *相邻* 。两个 `1` 之间的距离是它们的二进制表示中位置的绝对差。例如,`1001` 中的两个 `1` 的距离为 3。
99

10-
10+
*示例 1:*
1111

12+
....
13+
输入:n = 22
14+
输出:2
15+
解释:22 的二进制是 "10110" 。
16+
在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。
17+
第一对相邻的 1 中,两个 1 之间的距离为 2 。
18+
第二对相邻的 1 中,两个 1 之间的距离为 1 。
19+
答案取两个距离之中最大的,也就是 2 。
20+
....
1221

22+
*示例 2:*
1323

24+
....
25+
输入:n = 8
26+
输出:0
27+
解释:8 的二进制是 "1000" 。
28+
在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。
29+
....
1430

31+
*示例 3:*
1532

33+
....
34+
输入:n = 5
35+
输出:2
36+
解释:5 的二进制是 "101" 。
37+
....
1638

39+
*提示:*
1740

41+
* `1 \<= n \<= 10^9^`
1842
1943
2044
45+
== 思路分析
2146

22-
*Example 1:*
23-
24-
[subs="verbatim,quotes,macros"]
25-
----
26-
*Input:* 22
27-
*Output:* 2
28-
*Explanation:*
29-
22 in binary is 0b10110.
30-
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
31-
The first consecutive pair of 1's have distance 2.
32-
The second consecutive pair of 1's have distance 1.
33-
The answer is the largest of these two distances, which is 2.
34-
----
35-
36-
37-
*Example 2:*
38-
39-
[subs="verbatim,quotes,macros"]
40-
----
41-
*Input:* 5
42-
*Output:* 2
43-
*Explanation:*
44-
5 in binary is 0b101.
45-
----
46-
47-
48-
*Example 3:*
49-
50-
[subs="verbatim,quotes,macros"]
51-
----
52-
*Input:* 6
53-
*Output:* 1
54-
*Explanation:*
55-
6 in binary is 0b110.
56-
----
57-
58-
59-
*Example 4:*
60-
61-
[subs="verbatim,quotes,macros"]
62-
----
63-
*Input:* 8
64-
*Output:* 0
65-
*Explanation:*
66-
8 in binary is 0b1000.
67-
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.
68-
----
69-
70-
71-
72-
73-
74-
75-
*Note:*
76-
77-
78-
* `1 <= N <= 10^9`
79-
80-
81-
82-
83-
84-
85-
86-
87-
88-
47+
双指针:记录上一个 `1`,寻找下一个 `1`,然后计算两者的距离。
8948

9049
[[src-0868]]
50+
[tabs]
51+
====
52+
一刷::
53+
+
54+
--
9155
[{java_src_attr}]
9256
----
9357
include::{sourcedir}/_0868_BinaryGap.java[tag=answer]
9458
----
59+
--
60+
61+
// 二刷::
62+
// +
63+
// --
64+
// [{java_src_attr}]
65+
// ----
66+
// include::{sourcedir}/_0868_BinaryGap_2.java[tag=answer]
67+
// ----
68+
// --
69+
====
70+
71+
72+
== 参考资料
9573

74+
. https://leetcode.cn/problems/binary-gap/solutions/3906088/ji-suan-wei-ling-ge-shu-pythonjavacgocjs-jwgs/[868. 二进制间距 - 计算尾零个数^]
75+
. https://leetcode.cn/problems/binary-gap/solutions/1441893/er-jin-zhi-jian-ju-by-leetcode-solution-dh2q/[868. 二进制间距 - 官方题解^]

docs/images/binar-fundamentals.png

865 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ include::0866-prime-palindrome.adoc[leveloffset=+1]
18281828

18291829
include::0867-transpose-matrix.adoc[leveloffset=+1]
18301830

1831-
// include::0868-binary-gap.adoc[leveloffset=+1]
1831+
include::0868-binary-gap.adoc[leveloffset=+1]
18321832

18331833
include::0869-reordered-power-of-2.adoc[leveloffset=+1]
18341834

logbook/202601.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,12 @@ endif::[]
11761176
|{doc_base_url}/0867-transpose-matrix.adoc[题解]
11771177
|✅ 模拟题。矩阵遍历。
11781178

1179+
|{counter:codes}
1180+
|{leetcode_base_url}/binary-gap/[868. Binary Gap^]
1181+
|{doc_base_url}/0868-binary-gap.adoc[题解]
1182+
|✅ 数学,位运算,双指针。记录上一个 `1`,寻找下一个 `1`,然后计算两者的距离。
1183+
1184+
11791185

11801186

11811187
|===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0868_BinaryGap {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2026-08-17 22:24:28
9+
*/
10+
public int binaryGap(int n) {
11+
int result = 0;
12+
int temp = 0;
13+
while (n > 0) {
14+
if ((n & 1) == 1) {
15+
result = Math.max(result, temp);
16+
temp = 1;
17+
} else {
18+
if (temp > 0) {
19+
temp++;
20+
}
21+
}
22+
n >>= 1;
23+
}
24+
return result;
25+
}
26+
27+
// end::answer[]
28+
static void main() {
29+
new _0868_BinaryGap().binaryGap(8);
30+
}
31+
}

0 commit comments

Comments
 (0)