From f6d6db49b536c702414c3e11de0f6a29e52dc7d5 Mon Sep 17 00:00:00 2001 From: Jody Zhou <56443135+JodyZ0203@users.noreply.github.com> Date: Wed, 11 Nov 2020 20:13:30 -0500 Subject: [PATCH] =?UTF-8?q?Update=20=E5=B8=B8=E7=94=A8=E7=9A=84=E4=BD=8D?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 位1的个数python3 --- ...04\344\275\215\346\223\215\344\275\234.md" | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" index f5b5771..3601053 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" @@ -172,4 +172,27 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel

-======其他语言代码====== \ 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 + +''' -- GitLab