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 1/4] =?UTF-8?q?Update=20=E5=B8=B8=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 位1的个数python3 --- 算法思维系列/常用的位操作.md | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/算法思维系列/常用的位操作.md b/算法思维系列/常用的位操作.md index f5b5771..3601053 100644 --- a/算法思维系列/常用的位操作.md +++ b/算法思维系列/常用的位操作.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 + +''' From 31488adb9628132308d4913f965a74d1be6f49c7 Mon Sep 17 00:00:00 2001 From: Jody Zhou <56443135+JodyZ0203@users.noreply.github.com> Date: Wed, 11 Nov 2020 20:17:37 -0500 Subject: [PATCH 2/4] =?UTF-8?q?Update=20=E5=B8=B8=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 算法思维系列/常用的位操作.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/算法思维系列/常用的位操作.md b/算法思维系列/常用的位操作.md index 3601053..f056558 100644 --- a/算法思维系列/常用的位操作.md +++ b/算法思维系列/常用的位操作.md @@ -174,7 +174,7 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel ======其他语言代码====== -由[JodyZ203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码: +由[JodyZ0203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码: '''Python From f0332aa4f4125d0d62db52535c3de863c2462dd8 Mon Sep 17 00:00:00 2001 From: Jody Zhou <56443135+JodyZ0203@users.noreply.github.com> Date: Wed, 11 Nov 2020 22:53:37 -0500 Subject: [PATCH 3/4] =?UTF-8?q?Update=20=E5=B8=B8=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 算法思维系列/常用的位操作.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/算法思维系列/常用的位操作.md b/算法思维系列/常用的位操作.md index f056558..ebcde78 100644 --- a/算法思维系列/常用的位操作.md +++ b/算法思维系列/常用的位操作.md @@ -176,8 +176,7 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel 由[JodyZ0203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码: -'''Python - +```Python3 class Solution: def hammingWeight(self, n: int) -> int: @@ -194,5 +193,4 @@ class Solution: # 当二进制串全消除完之后,返回1出现的总数量 return count - -''' + ``` From 696741235cabd9d6bfc4a919f933c254c5817525 Mon Sep 17 00:00:00 2001 From: Jody Zhou <56443135+JodyZ0203@users.noreply.github.com> Date: Thu, 12 Nov 2020 01:47:31 -0500 Subject: [PATCH 4/4] =?UTF-8?q?Update=20=E5=B8=B8=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 算法思维系列/常用的位操作.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/算法思维系列/常用的位操作.md b/算法思维系列/常用的位操作.md index ebcde78..98f3e4b 100644 --- a/算法思维系列/常用的位操作.md +++ b/算法思维系列/常用的位操作.md @@ -176,7 +176,7 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel 由[JodyZ0203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码: -```Python3 +```Python class Solution: def hammingWeight(self, n: int) -> int: