feat: add initial implementation of Hamming weight function
This commit is contained in:
parent
8bc5d237f3
commit
b55dc00dcc
7
191_number_of_1_bits/Cargo.lock
generated
Normal file
7
191_number_of_1_bits/Cargo.lock
generated
Normal 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"
|
6
191_number_of_1_bits/Cargo.toml
Normal file
6
191_number_of_1_bits/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "number_of_1_bits"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
18
191_number_of_1_bits/src/main.rs
Normal file
18
191_number_of_1_bits/src/main.rs
Normal 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user