20240726 Finished
This commit is contained in:
		@@ -184,4 +184,5 @@ mod p502_ipo;
 | 
			
		||||
mod p373_find_k_pairs_with_smallest_sums;
 | 
			
		||||
mod p295_find_median_from_data_stream;
 | 
			
		||||
mod p67_add_binary;
 | 
			
		||||
mod p190_reverse_bits;
 | 
			
		||||
mod p190_reverse_bits;
 | 
			
		||||
mod p191_number_of_1_bits;
 | 
			
		||||
							
								
								
									
										68
									
								
								src/problem/p191_number_of_1_bits.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/problem/p191_number_of_1_bits.rs
									
									
									
									
									
										Normal 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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user