A few problems
This commit is contained in:
12
number-of-1-bits/sol.rs
Normal file
12
number-of-1-bits/sol.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user