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 e1073f9 commit ee3fda3Copy full SHA for ee3fda3
โnumber-of-1-bits/liza0525.pyโ
@@ -0,0 +1,17 @@
1
+class Solution:
2
+ # ์ด๋ค ์๋ฅผ 2์ง์๋ก ๋ณํํ๋ ๊ณผ์ ์ 2๋ก ๋๋ ํ, ๊ทธ ๋๋จธ์ง(1 ๋๋ 0)์ ๊ฐ์ฅ ๋์ ์๋ฆฌ ์๋ถํฐ ์์ฐจ๋ก ๋์ดํ๊ณ
3
+ # ๋ชซ์ ๊ทธ ๋ค์ ํ์ฐจ์ ํผ์ ์๋ก ์ฌ์ฉํ๋ฉด ๋๋ค.
4
+ # ํด๋น ๋ฌธ์ ๋ ๋์ดํ์ง ์๊ณ ๋์ ์๋ฅผ 2๋ก ๋๋ ๋ ๋งค ํ์ฐจ์ ๋๋จธ์ง๋ฅผ ๋ํด์ค์ผ๋ก์จ ํ ์ ์๋ค.
5
+ def hammingWeight(self, n: int) -> int:
6
+ bits_sum = 0
7
+ while n >= 1:
8
+ # ๋ชจ๋๋ก ์ฐ์ฐ์ผ๋ก ๋๋จธ์ง ๊ณ์ฐํ๋ค.
9
+ remainder = n % 2
10
+
11
+ # ๋๋จธ์ง ๊ฐ์ ๊ฒฐ๊ณผ ๊ฐ์ ๋ํ๋ค.
12
+ bits_sum += remainder
13
14
+ # ๋ชซ์ ๋ค์ ํ์ฐจ ํผ์ ์๋ก ์ฌ์ฉํ๋ค.
15
+ n = n // 2
16
17
+ return bits_sum
0 commit comments