20240729 Finished
This commit is contained in:
parent
0fbc21599e
commit
17ee25130e
|
@ -188,3 +188,4 @@ mod p190_reverse_bits;
|
||||||
mod p191_number_of_1_bits;
|
mod p191_number_of_1_bits;
|
||||||
mod p136_single_number;
|
mod p136_single_number;
|
||||||
mod p137_single_number_ii;
|
mod p137_single_number_ii;
|
||||||
|
mod p201_bitwise_and_of_numbers_range;
|
32
src/problem/p201_bitwise_and_of_numbers_range.rs
Normal file
32
src/problem/p201_bitwise_and_of_numbers_range.rs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user