Notice
Recent Posts
Recent Comments
- Today
- Total
01-11 01:45
Tags
- ๋ค์ต์คํธ๋ผ
- ์์์ ๋ ฌ
- BFS
- ์๋ฐ
- Algorithm
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- dp
- leetcode
- ์๋ฃ๊ตฌ์กฐ
- array
- pytorch
- java
- CS
- database
- ๋ฒจ๋งํฌ๋
- ๋ฌธ๋ฒ
- ๊ตฌํ
- ์ธํด
- MST
- ์๋ฐ์์ ์
- ์กธ์ ์ํ
- Graph
- ํ๋ก๊ทธ๋๋จธ์ค
- tree
- PS
- ๊ทธ๋ฆฌ๋
- ๋ฐฑ์๋
- spring
- OOP
- ๋ฐฑ์ค
Link
Partially Committed
[LEETCODE] 191.Number of 1 Bits ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
SMALL
TITLE : 191.Number of 1 Bits
Description : Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
2์ง์๋ก ๋ํ๋์ ๋, 1 ์ ๊ฐ์๋ฅผ ์ธ์ด์ฃผ๋ฉด ๋๋ค.
uint32_t n ์ ์ ๋ ฅ๋ฐ์ผ๋ฉด, n ์ด 0์ด ์๋ ๋ ๊น์ง 1 ๊ณผ and operator ์ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ans ์ ๋์ ํ๊ณ n ์ right shift ํด์ ๊ฐฑ์ ํด์ฃผ๋ฉด ๋๋ค.
ํน์ bitset ์ ์ด์ฉํ๋ฉด n bitset < 32 > (n).count(); ์ผ๋ก ์ฝ๊ฒ ๊ตฌํ ์๋ ์๋ค.
[C++]
class Solution {
public:
int hammingWeight(uint32_t n) {
int ans = 0;
while (n != 0) {
ans += (n & 1);
n = n >> 1;
}
return ans;
}
};
728x90
๋ฐ์ํ
LIST
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LEETCODE] 1779.Find Nearest Point That Has the Same X or Y Coordinate (0) | 2022.10.11 |
---|---|
[LEETCODE] 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2022.10.10 |
[LEETCODE] 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2022.10.09 |
[LEETCODE] 1523. Count Odd Numbers in an Interval Range (0) | 2022.10.09 |
[CH01] ํฌํฌ์ธํฐ (0) | 2022.09.12 |
Comments