leet-code/number-of-1-bits/sol.rs

12 lines
309 B
Rust
Raw Normal View History

2022-05-29 19:21:38 +00:00
impl Solution {
pub fn hammingWeight (n: u32) -> i32 {
(0..32).into_iter()
.fold(0, |acc, p| {
let compare_bit = 1 << p;
if n & compare_bit == compare_bit {
return acc + 1;
}
acc
})
}
}