Skip to content

Commit 8c4c27a

Browse files
committed
update: 添加问题“762.二进制表示中质数个计算置位”的代码和题解 + docs: github cli + windows credentials remove (#1403)
762: AC.cpp (#1402) - AC,67.64%,43.37% + en Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 7adf0fc commit 8c4c27a

14 files changed

Lines changed: 384 additions & 10 deletions

.commitTitleExtra

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+ docs: github cli + windows credentials remove

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
762: AC.cpp (#1402) - AC,67.64%,43.37% + en
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-21 12:13:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-21 12:23:11
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS CANNOT BE ACCEPTED
12+
class Solution {
13+
private:
14+
constexpr static int mask = 665772;
15+
public:
16+
int countPrimeSetBits(int left, int right) {
17+
int ans = 0;
18+
for (int i = left; i <= right; i++) {
19+
if (mask && 1 << __builtin_popcount(i)) {
20+
ans++;
21+
}
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-02-21 12:13:32
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-02-21 12:22:44
6+
'''
7+
max_length = 0
8+
while 1 << max_length < 1000000:
9+
max_length += 1
10+
print(f'max_length: {max_length}, 1 << {max_length} = {1 << max_length}')
11+
12+
primes = []
13+
for i in range(2, max_length + 1):
14+
isnot = False
15+
for j in range(2, i):
16+
if i % j == 0:
17+
isnot = True
18+
break
19+
if not isnot:
20+
primes.append(i)
21+
print(f'primes: {primes}')
22+
23+
mask = 0
24+
for p in primes:
25+
mask |= 1 << p
26+
print(f'mask: {mask}')
27+
28+
"""
29+
max_length: 20, 1 << 20 = 1048576
30+
primes: [2, 3, 5, 7, 11, 13, 17, 19]
31+
mask: 665772
32+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-21 12:13:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-21 12:28:09
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
constexpr static int mask = 665772;
14+
public:
15+
int countPrimeSetBits(int left, int right) {
16+
int ans = 0;
17+
for (int i = left; i <= right; i++) {
18+
if (mask & (1 << __builtin_popcount(i))) {
19+
// printf("i = %d, cnt1 = %d\n", i, __builtin_popcount(i));
20+
ans++;
21+
}
22+
}
23+
return ans;
24+
}
25+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-02-03 22:57:38
5+
* @LastEditTime: 2026-02-21 11:12:11
66
-->
77
# LetLeet Blog
88

@@ -151,6 +151,7 @@
151151
|Windows - Windows10绕过登录(修改)密码登录:未验证|<a href="https://blog.letmefly.xyz/2023/07/05/Other-Windows-LoginWindowsWithoutPassword/">本平台博客</a>|无|
152152
|Windows(奇思妙想) - 通过ssh打开带有图形界面的程序 - 一种通过计划任务的曲折实现方式|<a href="https://blog.letmefly.xyz/2025/02/21/Other-Windows-OpenUIbySSH">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/145786262">CSDN博客</a>|
153153
|Windows程序互斥锁 - 一个程序同时仅允许运行一个实例|<a href="https://blog.letmefly.xyz/2024/02/17/Other-Windows-ProgramMutex-onlyRun1instanceMeanwhile/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/136139822">CSDN博客</a>|
154+
|Windows - VsCode导致Windows凭据过多之一键删除|<a href="https://blog.letmefly.xyz/2026/02/21/Other-Windows-RemoveExcessWindowsCredentialsGeneratedByVsCode/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/158262243">CSDN博客</a>|
154155
|Windows - URL Scheme - 在Windows上无管理员权限为你的程序添加URL Scheme|<a href="https://blog.letmefly.xyz/2024/02/08/Other-Windows-URLScheme-Define1URLSchemeForYourApplicationWithoutUACOnWindows/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/136083203">CSDN博客</a>|
155156
|Windows - UWP - 为UWP应用创建桌面快捷方式|<a href="https://blog.letmefly.xyz/2023/08/12/Other-Windows-UWP-createShortcutOnDesktop/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/132254567">CSDN博客</a>|
156157
|Windows - UWP - 网络不好的情况下安装(微软商店)MicrosoftStore的应用|<a href="https://blog.letmefly.xyz/2023/08/13/Other-Windows-UWP-InstallingUWPinMSStoreUsingTroubleInternet/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/132257018">CSDN博客</a>|
@@ -410,6 +411,7 @@
410411
|0754.到达终点数字|中等|<a href="https://leetcode.cn/problems/reach-a-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/04/LeetCode%200754.%E5%88%B0%E8%BE%BE%E7%BB%88%E7%82%B9%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127684453" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reach-a-number/solutions/1947583/letmefly-754dao-da-zhong-dian-shu-zi-by-z6q4r/" target="_blank">LeetCode题解</a>|
411412
|0756.金字塔转换矩阵|中等|<a href="https://leetcode.cn/problems/pyramid-transition-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/29/LeetCode%200756.%E9%87%91%E5%AD%97%E5%A1%94%E8%BD%AC%E6%8D%A2%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156400643" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pyramid-transition-matrix/solutions/3868875/letmefly-756jin-zi-ta-zhuan-huan-ju-zhen-ldxx/" target="_blank">LeetCode题解</a>|
412413
|0761.特殊的二进制字符串|困难|<a href="https://leetcode.cn/problems/special-binary-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/20/LeetCode%200761.%E7%89%B9%E6%AE%8A%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158235045" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/special-binary-string/solutions/3905163/letmefly-761te-shu-de-er-jin-zhi-zi-fu-c-q26c/" target="_blank">LeetCode题解</a>|
414+
|0762.二进制表示中质数个计算置位|简单|<a href="https://leetcode.cn/problems/prime-number-of-set-bits-in-binary-representation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/21/LeetCode%200762.%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E4%B8%AD%E8%B4%A8%E6%95%B0%E4%B8%AA%E8%AE%A1%E7%AE%97%E7%BD%AE%E4%BD%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158262877" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/prime-number-of-set-bits-in-binary-representation/solutions/3905643/letmefly-762er-jin-zhi-biao-shi-zhong-zh-d3jl/" target="_blank">LeetCode题解</a>|
413415
|0765.情侣牵手|困难|<a href="https://leetcode.cn/problems/couples-holding-hands/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/11/LeetCode%200765.%E6%83%85%E4%BE%A3%E7%89%B5%E6%89%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134355602" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/couples-holding-hands/solutions/2522691/letmefly-765qing-lu-qian-shou-yan-du-you-nt6z/" target="_blank">LeetCode题解</a>|
414416
|0769.最多能完成排序的块|中等|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/13/LeetCode%200769.%E6%9C%80%E5%A4%9A%E8%83%BD%E5%AE%8C%E6%88%90%E6%8E%92%E5%BA%8F%E7%9A%84%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127295302" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/solution/letmefly-769zui-duo-neng-wan-cheng-pai-x-pzsz/" target="_blank">LeetCode题解</a>|
415417
|0771.宝石与石头|简单|<a href="https://leetcode.cn/problems/jewels-and-stones/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/24/LeetCode%200771.%E5%AE%9D%E7%9F%B3%E4%B8%8E%E7%9F%B3%E5%A4%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131888350" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/jewels-and-stones/solutions/2356279/letmefly-771bao-shi-yu-shi-tou-by-tisfy-fqmc/" target="_blank">LeetCode题解</a>|
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: 762.二进制表示中质数个计算置位:位运算(mask O(1)判断)
3+
date: 2026-02-21 12:30:01
4+
tags: [题解, LeetCode, 简单, 位运算, 数学, 质数]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】762.二进制表示中质数个计算置位:位运算(mask O(1)判断)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/prime-number-of-set-bits-in-binary-representation/](https://leetcode.cn/problems/prime-number-of-set-bits-in-binary-representation/)
11+
12+
<p>给你两个整数&nbsp;<code>left</code>&nbsp;&nbsp;<code>right</code> ,在闭区间 <code>[left, right]</code>&nbsp;范围内,统计并返回 <strong>计算置位位数为质数</strong> 的整数个数。</p>
13+
14+
<p><strong>计算置位位数</strong> 就是二进制表示中 <code>1</code> 的个数。</p>
15+
16+
<ul>
17+
<li>例如, <code>21</code>&nbsp;的二进制表示&nbsp;<code>10101</code>&nbsp;有 <code>3</code> 个计算置位。</li>
18+
</ul>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>left = 6, right = 10
26+
<strong>输出:</strong>4
27+
<strong>解释:</strong>
28+
6 -&gt; 110 (2 个计算置位,2 是质数)
29+
7 -&gt; 111 (3 个计算置位,3 是质数)
30+
9 -&gt; 1001 (2 个计算置位,2 是质数)
31+
10-&gt; 1010 (2 个计算置位,2 是质数)
32+
共计 4 个计算置位为质数的数字。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>left = 10, right = 15
39+
<strong>输出:</strong>5
40+
<strong>解释:</strong>
41+
10 -&gt; 1010 (2 个计算置位, 2 是质数)
42+
11 -&gt; 1011 (3 个计算置位, 3 是质数)
43+
12 -&gt; 1100 (2 个计算置位, 2 是质数)
44+
13 -&gt; 1101 (3 个计算置位, 3 是质数)
45+
14 -&gt; 1110 (3 个计算置位, 3 是质数)
46+
15 -&gt; 1111 (4 个计算置位, 4 不是质数)
47+
共计 5 个计算置位为质数的数字。
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
52+
<p><strong>提示:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= left &lt;= right &lt;= 10<sup>6</sup></code></li>
56+
<li><code>0 &lt;= right - left &lt;= 10<sup>4</sup></code></li>
57+
</ul>
58+
59+
60+
61+
## 解题方法:预处理
62+
63+
写个脚本,计算$10^6$范围内二进制下最多有多少个1:$1^{20}=1048576$
64+
65+
再算出来其中都有哪些是质数:$[2, 3, 5, 7, 11, 13, 17, 19]$
66+
67+
再使用一个整数二进制下的每一位代表一个数是否是质数。
68+
69+
```python
70+
'''
71+
LastEditTime: 2026-02-21 12:22:44
72+
'''
73+
max_length = 0
74+
while 1 << max_length < 1000000:
75+
max_length += 1
76+
print(f'max_length: {max_length}, 1 << {max_length} = {1 << max_length}')
77+
78+
primes = []
79+
for i in range(2, max_length + 1):
80+
isnot = False
81+
for j in range(2, i):
82+
if i % j == 0:
83+
isnot = True
84+
break
85+
if not isnot:
86+
primes.append(i)
87+
print(f'primes: {primes}')
88+
89+
mask = 0
90+
for p in primes:
91+
mask |= 1 << p
92+
print(f'mask: {mask}')
93+
94+
"""
95+
max_length: 20, 1 << 20 = 1048576
96+
primes: [2, 3, 5, 7, 11, 13, 17, 19]
97+
mask: 665772
98+
"""
99+
```
100+
101+
+ 时间复杂度$O(right - left)$
102+
+ 空间复杂度$O(N\log N)$
103+
104+
### AC代码
105+
106+
#### C++
107+
108+
```cpp
109+
/*
110+
* @LastEditTime: 2026-02-21 12:28:09
111+
*/
112+
class Solution {
113+
private:
114+
constexpr static int mask = 665772;
115+
public:
116+
int countPrimeSetBits(int left, int right) {
117+
int ans = 0;
118+
for (int i = left; i <= right; i++) {
119+
if (mask & (1 << __builtin_popcount(i))) {
120+
// printf("i = %d, cnt1 = %d\n", i, __builtin_popcount(i));
121+
ans++;
122+
}
123+
}
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158262877)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/21/LeetCode%200762.%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA%E4%B8%AD%E8%B4%A8%E6%95%B0%E4%B8%AA%E8%AE%A1%E7%AE%97%E7%BD%AE%E4%BD%8D/)哦~
130+
>
131+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,36 @@ git fetch machine1
308308
git config receive.denyCurrentBranch updateInstead
309309
```
310310

311+
### About Github GH(Github CLI)
312+
313+
官网:[cli.github.com](https://cli.github.com/)
314+
315+
安装:
316+
317+
* Mac:
318+
319+
```bash
320+
brew install gh
321+
# brew upgrade gh
322+
```
323+
324+
* Windows:
325+
326+
[Releases](https://github.com/cli/cli/releases)点`show all xx asserts`,找到`GitHub CLI 2.87.2 windows amd64`下载并添加到环境变量path中;或者找`GitHub CLI 2.87.2 windows amd64 installer`傻瓜式下一步安装。
327+
328+
登录:
329+
330+
```bash
331+
gh auth login
332+
# 可登录多账号,之后使用gh switch USERNAME切换
333+
```
334+
335+
添加project权限:
336+
337+
```bash
338+
gh auth refresh -s project
339+
```
340+
311341
## About HTML
312342

313343
### 空白字符

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,8 @@ categories: [自用]
18251825
|infringe|v. 侵犯,违背|
18261826
|||
18271827
|mumps|n. 腮腺炎|
1828+
|||
1829+
|praise|v. 赞扬<br/>n. 赞扬|
18281830

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

0 commit comments

Comments
 (0)