File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ 1. n์ด 11์ผ๋, ์ด์ง์๋ก ๋ณํํด๋ณด๋ฉด 1011์ด๋ค. set bits๋ 1์ ๊ฐ์ ์ฐพ๋ ๊ฒ์ด๊ธฐ์ 1์ ์นด์ดํธํด์ค๋ค.
4
+ ์๊ฐ ๋ณต์ก๋ (Time Complexity):
5
+ - bin(n): ์ ์๋ฅผ ์ด์ง ๋ฌธ์์ด๋ก ๋ณํ โ O(log n)
6
+ (n์ ํฌ๊ธฐ์ ๋ฐ๋ผ ํ์ํ ๋นํธ ์๋งํผ ์ฐ์ฐํจ)
7
+ - count('1'): ๋ฌธ์์ด์์ '1'์ ๊ฐ์๋ฅผ ์ธ๊ธฐ ์ํด ์ ์ฒด ์ํ โ O(log n)
8
+ (์ด์ง ๋ฌธ์์ด์ ๊ธธ์ด๋ logโ(n)์ ๋น๋ก)
9
+ ์ต์ข
์๊ฐ ๋ณต์ก๋: O(log n)
10
+
11
+ ๊ณต๊ฐ ๋ณต์ก๋ (Space Complexity):
12
+ - bin(n)์ ๊ฒฐ๊ณผ๋ก ์์ฑ๋ ์ด์ง ๋ฌธ์์ด์ ์ ์ฅ โ O(log n)
13
+ - ๊ทธ ์ธ ๋ณ๋์ ์ถ๊ฐ ๊ณต๊ฐ ์์
14
+ ์ต์ข
๊ณต๊ฐ ๋ณต์ก๋: O(log n)
15
+ """
16
+ def hammingWeight (self , n : int ) -> int :
17
+ return bin (n ).count ('1' )
You canโt perform that action at this time.
0 commit comments