File tree Expand file tree Collapse file tree 1 file changed +18
-5
lines changed
Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Original file line number Diff line number Diff line change 11// Time: O(1)
22// Space: O(1)
33
4- pub struct Solution { }
4+ pub struct Solution1 { }
5+ pub struct Solution2 { }
56
6- impl Solution {
7- pub fn range_bitwise_and ( m : i32 , mut n : i32 ) -> i32 {
7+ impl Solution1 {
8+ pub fn range_bitwise_and ( m : u32 , mut n : u32 ) -> u32 {
9+ while m < n { // Remove the last bit 1 until n <= m.
10+ n &= n - 1 ;
11+ }
12+ n
13+ }
14+ }
15+
16+ impl Solution2 {
17+ pub fn range_bitwise_and ( m : u32 , mut n : u32 ) -> u32 {
818 while m < n { // Remove the last bit 1 until n <= m.
919 n &= n - 1 ;
1020 }
@@ -18,7 +28,10 @@ mod tests {
1828
1929 #[ test]
2030 fn test_range_bitwise_and ( ) {
21- assert_eq ! ( Solution :: range_bitwise_and( 5 , 7 ) , 4 ) ;
22- assert_eq ! ( Solution :: range_bitwise_and( 0 , 1 ) , 0 ) ;
31+ assert_eq ! ( Solution1 :: range_bitwise_and( 5 , 7 ) , 4 ) ;
32+ assert_eq ! ( Solution1 :: range_bitwise_and( 0 , 1 ) , 0 ) ;
33+
34+ assert_eq ! ( Solution2 :: range_bitwise_and( 5 , 7 ) , 4 ) ;
35+ assert_eq ! ( Solution2 :: range_bitwise_and( 0 , 1 ) , 0 ) ;
2336 }
2437}
You can’t perform that action at this time.
0 commit comments