Skip to content

Commit 5857f55

Browse files
committed
update: 添加问题“868.二进制间距”的代码和题解 (#1405)
Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 8c4c27a commit 5857f55

11 files changed

Lines changed: 144 additions & 34 deletions

.commitTitleExtra

Lines changed: 0 additions & 1 deletion
This file was deleted.

.commitmsg

Lines changed: 0 additions & 1 deletion
This file was deleted.

Codes/0762-prime-number-of-set-bits-in-binary-representation_20260221.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
* @Author: LetMeFly
33
* @Date: 2026-02-21 12:13:32
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-02-21 12:23:11
5+
* @LastEditTime: 2026-02-21 12:28:09
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS CANNOT BE ACCEPTED
1211
class Solution {
1312
private:
1413
constexpr static int mask = 665772;
1514
public:
1615
int countPrimeSetBits(int left, int right) {
1716
int ans = 0;
1817
for (int i = left; i <= right; i++) {
19-
if (mask && 1 << __builtin_popcount(i)) {
18+
if (mask & (1 << __builtin_popcount(i))) {
19+
// printf("i = %d, cnt1 = %d\n", i, __builtin_popcount(i));
2020
ans++;
2121
}
2222
}

Codes/0762-prime-number-of-set-bits-in-binary-representation_20260221_AC.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

Codes/0868-binary-gap_20260222.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-22 09:59:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-22 10:04:00
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int binaryGap(int n) {
14+
n /= (n & -n) * 2;
15+
int ans = 0, cnt = 1;
16+
while (n) {
17+
if (n & 1) {
18+
ans = max(ans, cnt);
19+
cnt = 0;
20+
}
21+
cnt++;
22+
n >>= 1; // Don't forget
23+
}
24+
return ans;
25+
}
26+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@
448448
|0860.柠檬水找零|简单|<a href="https://leetcode.cn/problems/lemonade-change/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/22/LeetCode%200860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131864033" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lemonade-change/solutions/2353759/letmefly-860ning-meng-shui-zhao-ling-fu-5v6m1/" target="_blank">LeetCode题解</a>|
449449
|0864.获取所有钥匙的最短路径|困难|<a href="https://leetcode.cn/problems/shortest-path-to-get-all-keys/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/10/LeetCode%200864.%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E9%92%A5%E5%8C%99%E7%9A%84%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127784094" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-path-to-get-all-keys/solutions/1960697/letmefly-864huo-qu-suo-you-yao-chi-de-zu-urtg/" target="_blank">LeetCode题解</a>|
450450
|0865.具有所有最深节点的最小子树|中等|<a href="https://leetcode.cn/problems/smallest-subtree-with-all-the-deepest-nodes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/09/LeetCode%200865.%E5%85%B7%E6%9C%89%E6%89%80%E6%9C%89%E6%9C%80%E6%B7%B1%E8%8A%82%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%8F%E5%AD%90%E6%A0%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156773047" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/smallest-subtree-with-all-the-deepest-nodes/solutions/3876835/letmefly-865ju-you-suo-you-zui-shen-jie-bg80w/" target="_blank">LeetCode题解</a>|
451+
|0868.二进制间距|简单|<a href="https://leetcode.cn/problems/binary-gap/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/22/LeetCode%200868.%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%97%B4%E8%B7%9D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158283908" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/binary-gap/solutions/3906141/letmefly-868er-jin-zhi-jian-ju-wei-yun-s-jo6u/" target="_blank">LeetCode题解</a>|
451452
|0869.重新排序得到2的幂|中等|<a href="https://leetcode.cn/problems/reordered-power-of-2/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/10/LeetCode%200869.%E9%87%8D%E6%96%B0%E6%8E%92%E5%BA%8F%E5%BE%97%E5%88%B02%E7%9A%84%E5%B9%82/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150158685" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reordered-power-of-2/solutions/3749023/letmefly-869zhong-xin-pai-xu-de-dao-2-de-sd1j/" target="_blank">LeetCode题解</a>|
452453
|0870.优势洗牌|中等|<a href="https://leetcode.cn/problems/advantage-shuffle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/08/LeetCode%200870.%E4%BC%98%E5%8A%BF%E6%B4%97%E7%89%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127207642" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/advantage-shuffle/solution/letmefly-qu-jie-tian-ji-sai-ma-neng-ying-d9qu/" target="_blank">LeetCode题解</a>|
453454
|0871.最低加油次数|困难|<a href="https://leetcode.cn/problems/minimum-number-of-refueling-stops/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/02/LeetCode%200871.%E6%9C%80%E4%BD%8E%E5%8A%A0%E6%B2%B9%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125575683" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-refueling-stops/solution/letmefly-871zui-di-jia-you-ci-shu-lei-si-ucry/" target="_blank">LeetCode题解</a>|
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 868.二进制间距:位运算
3+
date: 2026-02-22 10:05:37
4+
tags: [题解, LeetCode, 简单, 位运算]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】868.二进制间距:位运算
9+
10+
力扣题目链接:[https://leetcode.cn/problems/binary-gap/](https://leetcode.cn/problems/binary-gap/)
11+
12+
<p>给定一个正整数 <code>n</code>,找到并返回 <code>n</code> 的二进制表示中两个 <strong>相邻</strong> 1 之间的<strong> 最长距离 </strong>。如果不存在两个相邻的 1,返回 <code>0</code> 。</p>
13+
14+
<p>如果只有 <code>0</code> 将两个 <code>1</code> 分隔开(可能不存在 <code>0</code> ),则认为这两个 1 彼此 <strong>相邻</strong> 。两个 <code>1</code> 之间的距离是它们的二进制表示中位置的绝对差。例如,<code>"1001"</code> 中的两个 <code>1</code> 的距离为 3 。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<ul>
19+
</ul>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>n = 22
25+
<strong>输出:</strong>2
26+
<strong>解释:</strong>22 的二进制是 "10110" 。
27+
在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。
28+
第一对相邻的 1 中,两个 1 之间的距离为 2 。
29+
第二对相邻的 1 中,两个 1 之间的距离为 1 。
30+
答案取两个距离之中最大的,也就是 2 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>n = 8
37+
<strong>输出:</strong>0
38+
<strong>解释:</strong>8 的二进制是 "1000" 。
39+
在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。
40+
</pre>
41+
42+
<p><strong>示例 3:</strong></p>
43+
44+
<pre>
45+
<strong>输入:</strong>n = 5
46+
<strong>输出:</strong>2
47+
<strong>解释:</strong>5 的二进制是 "101" 。
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
52+
<p><strong>提示:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
56+
</ul>
57+
58+
59+
60+
## 解题方法:位运算
61+
62+
以样例一的二进制`n = 10110`为例,我们只需要在`n`非零时使用一个变量`cnt`记录遇到下一个1之前一共有几位就好了,再次遇到`1`时更新答案最大值并重置`cnt`
63+
64+
```cpp
65+
while (n) {
66+
if (n & 1) {
67+
ans = max(ans, cnt);
68+
cnt = 0;
69+
}
70+
cnt++;
71+
n >>= 1; // Don't forget
72+
}
73+
```
74+
75+
但是问题在于如何得到第一个`1`的位置,很简单,可以借助[`lowbit`](https://web.letmefly.xyz/Notes/ACM/Template/lowbit.html)的思想,`n & -n`得到最低位的`1`及后面的`0``lowbit(10110) = 10`)令`n / lowbit(n)`即相当于抹掉了低位的所有`0``n / lowbit(n) >> 1`相当于抹掉了最低位的`1`及其右边的所有`0``10110 / lowbit(10110) >> 1 = 101`),这样就找到最低位`1`的位置了。
76+
77+
+ 时间复杂度$O(\log n)$
78+
+ 空间复杂度$O(1)$
79+
80+
### AC代码
81+
82+
#### C++
83+
84+
```cpp
85+
/*
86+
* @LastEditTime: 2026-02-22 10:04:00
87+
*/
88+
class Solution {
89+
public:
90+
int binaryGap(int n) {
91+
n /= (n & -n) * 2;
92+
int ans = 0, cnt = 1;
93+
while (n) {
94+
if (n & 1) {
95+
ans = max(ans, cnt);
96+
cnt = 0;
97+
}
98+
cnt++;
99+
n >>= 1; // Don't forget
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158283908)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/22/LeetCode%200868.%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%97%B4%E8%B7%9D/)哦~
107+
>
108+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ git config receive.denyCurrentBranch updateInstead
329329

330330
```bash
331331
gh auth login
332-
# 可登录多账号,之后使用gh switch USERNAME切换
332+
# 可登录多账号,之后使用gh auth switch -u USERNAME切换
333333
```
334334

335335
添加project权限:

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,8 @@ categories: [自用]
18271827
|mumps|n. 腮腺炎|
18281828
|||
18291829
|praise|v. 赞扬<br/>n. 赞扬|
1830+
|||
1831+
|stipulation|n. 规定,约定,合同|
18301832

18311833
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
18321834
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

Solutions/Other-Windows-RemoveExcessWindowsCredentialsGeneratedByVsCode.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ failed to move active token in keyring: Not enough memory resources are availabl
3333
3434
<script src="https://web.letmefly.xyz/Links/JS/GithubGists/RmWindowsCredentials/5fc6647e2f5289a656d2441229cdd392.js"></script>
3535
36-
<details><summary>script</summary>
36+
<details><summary>duplicated script(一键复制版本)</summary>
3737
3838
3939
```powershell
@@ -115,6 +115,8 @@ Read-Host "按回车退出"
115115
116116
删了当年留下的182个凭据后,`gh`表现正常了。
117117
118+
附:凭据位置 - `控制面板\所有控制面板项\凭据管理器`的`Windows凭据`
119+
118120
<center><font size="6px" face="Ink Free">The Real End, Thanks!</font></center>
119121
120122
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158262243)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/21/Other-Windows-RemoveExcessWindowsCredentialsGeneratedByVsCode/)哦~

0 commit comments

Comments
 (0)