20240726 Finished

This commit is contained in:
jackfiled 2024-07-26 11:12:53 +08:00
parent 207d8ff783
commit c88f7a8f79
2 changed files with 70 additions and 1 deletions

View File

@ -185,3 +185,4 @@ mod p373_find_k_pairs_with_smallest_sums;
mod p295_find_median_from_data_stream; mod p295_find_median_from_data_stream;
mod p67_add_binary; mod p67_add_binary;
mod p190_reverse_bits; mod p190_reverse_bits;
mod p191_number_of_1_bits;

View File

@ -0,0 +1,68 @@
/**
* [191] Number of 1 Bits
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn hamming_weight(n: i32) -> i32 {
static ARRAY: [i32; 31] = [
1 << 30,
1 << 29,
1 << 28,
1 << 27,
1 << 26,
1 << 25,
1 << 24,
1 << 23,
1 << 22,
1 << 21,
1 << 20,
1 << 19,
1 << 18,
1 << 17,
1 << 16,
1 << 15,
1 << 14,
1 << 13,
1 << 12,
1 << 11,
1 << 10,
1 << 9,
1 << 8,
1 << 7,
1 << 6,
1 << 5,
1 << 4,
1 << 3,
1 << 2,
1 << 1,
1
];
let mut result = 0;
for i in 0..31 {
if n & ARRAY[i] == ARRAY[i] {
result += 1;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_191() {
assert_eq!(3, Solution::hamming_weight(11));
assert_eq!(1, Solution::hamming_weight(128));
}
}