We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 764af12 commit 3a0a6ceCopy full SHA for 3a0a6ce
leetcode.com 191. Number of 1 Bits/main.py
@@ -0,0 +1,14 @@
1
+class Solution:
2
+ def hammingWeight(self, n: int) -> int:
3
+ answer = 0
4
+
5
+ while n > 1:
6
+ q, r = divmod(n, 2)
7
+ if r == 1:
8
+ answer += 1
9
+ n = q
10
11
+ if n == 1:
12
13
14
+ return answer
leetcode.com 191. Number of 1 Bits/test_main.py
@@ -0,0 +1,15 @@
+from unittest import TestCase
+from main import Solution
+class TestSolution(TestCase):
+ def test1_hamming_weight(self):
+ sln = Solution()
+ self.assertEqual(3, sln.hammingWeight(11))
+ def test2_hamming_weight(self):
+ self.assertEqual(1, sln.hammingWeight(128))
+ def test3_hamming_weight(self):
15
+ self.assertEqual(30, sln.hammingWeight(2147483645))
0 commit comments