Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions number-of-1-bits/DaleSeo.rs
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +3 to +12

@parkhojeong parkhojeong Jul 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 Brian Kernighan algorithm이라고도 불리는 방식이군요!

rust에선 count_ones()를 사용하면 cpu 의 popcount 가 지원되는 경우 명령어 사이클을 더 줄일 수도 있는 거 같네요 :)

}
Loading