Leetcode - Number of 1 Bits

violet posted @ Jun 26, 2020 02:19:00 AM in 算法 with tags Algorithm BitManipulation Golang , 301 阅读

https://leetcode.com/problems/number-of-1-bits/

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

Example 1:

```Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string `00000000000000000000000000001011 has a total of three '1' bits.`
```

```func hammingWeight(num uint32) int {
if num == 0 {
return 0
}
count := 0
for num != 0 {
num &= num - 1
count++
}
return count
}```
1st Inter Blueprint 说:
Feb 10, 2023 03:46:15 PM

The education board will be in charge of 11th-grade assessment. Download the Assessment Marking Scheme from the Board. Students may find all of the information they need about the exam New Marking Scheme in the article below. The board has also downloaded realistic assessment dates. The reasonable will be directed beginning in March 2024.1st Inter Blueprint 2024 The Council of Higher Secondary Education has announced the modified Class XI Blueprint for the 2024 Annual Examination.

(输入验证码)
or Ctrl+Enter