File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020#include < iostream>
2121using namespace std ;
2222
23+ // ์๊ฐ ๋ณต์ก๋ : O(logn) -> n >> 1 ์ฐ์ฐ์ ํ๊ธฐ ๋๋ฌธ์ ์ ๋ฐ์ฉ ์ค์ด๋ฌ
24+ // ๋ฌธ์ ์ ์กฐ๊ฑด์ 32bit ๋ด์ ๋ฒ์์ด๊ธฐ ๋๋ฌธ์ 32๋ฒ์ ๋ฐ๋ณต์ผ๋ก ํญ์ ๋๋๊ธฐ์ O(1)์ด๋ผ๊ณ ํ ์ ์๋ค.
25+ // ๊ณต๊ฐ ๋ณต์ก๋ : O(1)
2326class Solution {
2427public:
2528 int hammingweight (int n) {
@@ -29,14 +32,20 @@ class Solution {
2932 answer++;
3033 n = n >> 1 ;
3134 }
35+
36+ // ์ฒ์ ํ์๋ ํ์ด.
37+ // unsigned int answer = 0;
38+ // for (int i = 0; i < 32; i++) {
39+ // if ((n >> i) & 1) answer++;
40+ // }
41+
42+ // ์๊ฐ ๋ชป ํ ํ์ด
43+ // -> n & (n-1) ์ ํ๋ฉด ๊ฐ์ฅ ์ค๋ฅธ์ชฝ 1๋นํธ๋ฅผ ์ง์ด๋ค.
44+ // while (n > 0) {
45+ // n &= (n - 1);
46+ // answer++;
47+ // }
48+
3249 return answer;
3350 }
3451};
35-
36- int main () {
37- int n;
38- cin >> n;
39- Solution s;
40-
41- cout << s.hammingweight (n);
42- }
You canโt perform that action at this time.
0 commit comments