chapter_data_structure/exercises/ #1948
Replies: 4 comments
|
n & (n - 1) 会把 n 中最右边的一个 1 变成 0 |
0 replies
def count_ones(n: int) -> int:
count = 0
while n:
n &= n - 1 # 清除最低位的 1
count += 1
return count |
0 replies
def count_ones(n):
count = 0
while n:
n &= n - 1
count += 1
return count |
0 replies
int CountOne(int n) {
int count = 0;
for (int i = 0; i < 32; ++i) {
if (n & 1) {
count++;
n = n >> 1;
}
}
return count;
}
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
chapter_data_structure/exercises/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_data_structure/exercises/
All reactions