20240729 Finished

This commit is contained in:
jackfiled 2024-07-29 11:56:46 +08:00
parent 0fbc21599e
commit 17ee25130e
2 changed files with 34 additions and 1 deletions

View File

@ -187,4 +187,5 @@ mod p67_add_binary;
mod p190_reverse_bits;
mod p191_number_of_1_bits;
mod p136_single_number;
mod p137_single_number_ii;
mod p137_single_number_ii;
mod p201_bitwise_and_of_numbers_range;

View File

@ -0,0 +1,32 @@
/**
* [201] Bitwise AND of Numbers Range
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn range_bitwise_and(left: i32, right: i32) -> i32 {
let mut right = right;
while left < right {
right = right & (right - 1);
}
right
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_201() {
assert_eq!(4, Solution::range_bitwise_and(5, 7));
assert_eq!(0, Solution::range_bitwise_and(0, 0));
}
}