mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
19 lines
257 B
Go
19 lines
257 B
Go
package leetcode
|
|
|
|
import "math/bits"
|
|
|
|
// 解法一
|
|
func hammingWeight(num uint32) int {
|
|
return bits.OnesCount(uint(num))
|
|
}
|
|
|
|
// 解法二
|
|
func hammingWeight1(num uint32) int {
|
|
count := 0
|
|
for num != 0 {
|
|
num = num & (num - 1)
|
|
count++
|
|
}
|
|
return count
|
|
}
|