未验证 提交 f6d6db49 编写于 作者: J Jody Zhou 提交者: GitHub

Update 常用的位操作.md

位1的个数python3
上级 36f59b6f
......@@ -172,4 +172,27 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
[JodyZ203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码:
'''Python
class Solution:
def hammingWeight(self, n: int) -> int:
# 先定义一个count,用来存1的出现数量
count = 0
# 只要二进制串不等于0之前,我们用一个循环边消除1和计1的出现数量
while n!=0:
# 用labuladong在文章中所提到的 n&(n-1) 技巧来消除最后一个1
n = n & (n-1)
count+=1
# 当二进制串全消除完之后,返回1出现的总数量
return count
'''
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册