diff --git a/number-of-1-bits/DaleSeo.rs b/number-of-1-bits/DaleSeo.rs new file mode 100644 index 0000000000..5d567049e1 --- /dev/null +++ b/number-of-1-bits/DaleSeo.rs @@ -0,0 +1,13 @@ +// TC: O(k) +// SC: O(1) +impl Solution { + pub fn hamming_weight(n: i32) -> i32 { + let mut n = n as u32; + let mut cnt = 0; + while n != 0 { + n &= n - 1; + cnt += 1; + } + cnt + } +}