feat: add initial implementation of Hamming weight function

This commit is contained in:
SquidSpirit 2025-08-17 22:04:40 +08:00
parent 8bc5d237f3
commit b55dc00dcc
3 changed files with 31 additions and 0 deletions

7
191_number_of_1_bits/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "number_of_1_bits"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "number_of_1_bits"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,18 @@
fn main() {
println!("Hello, world!");
}
struct Solution;
impl Solution {
pub fn hamming_weight(mut n: i32) -> i32 {
let mut result = 0;
while n > 0 {
result += n & 1;
n >>= 1;
}
result
}
}