diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 06b3074..ee13a71 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; \ No newline at end of file +mod p137_single_number_ii; +mod p201_bitwise_and_of_numbers_range; \ No newline at end of file diff --git a/src/problem/p201_bitwise_and_of_numbers_range.rs b/src/problem/p201_bitwise_and_of_numbers_range.rs new file mode 100644 index 0000000..8604aef --- /dev/null +++ b/src/problem/p201_bitwise_and_of_numbers_range.rs @@ -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)); + } +}