Skip to content

Commit 0ded189

Browse files
authored
number-of-1-bits (#2721)
1 parent 5651493 commit 0ded189

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

number-of-1-bits/DaleSeo.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TC: O(k)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn hamming_weight(n: i32) -> i32 {
5+
let mut n = n as u32;
6+
let mut cnt = 0;
7+
while n != 0 {
8+
n &= n - 1;
9+
cnt += 1;
10+
}
11+
cnt
12+
}
13+
}

0 commit comments

Comments
 (0)