Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2252c28

Browse files
committed
fix range_bitwise_and
1 parent ff6f559 commit 2252c28

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
14
pub struct Solution {}
25

36
impl Solution {
4-
pub fn range_bitwise_and(_a: i32, _b: i32) -> i32 {
5-
0
7+
pub fn range_bitwise_and(m: i32, mut n: i32) -> i32 {
8+
while m < n { // Remove the last bit 1 until n <= m.
9+
n &= n - 1;
10+
}
11+
n
612
}
713
}
814

@@ -12,7 +18,7 @@ mod tests {
1218

1319
#[test]
1420
fn test_range_bitwise_and() {
15-
assert_eq!(Solution::range_bitwise_and(1, 2), 3);
16-
assert_ne!(Solution::range_bitwise_and(1, 2), 4);
21+
assert_eq!(Solution::range_bitwise_and(5, 7), 4);
22+
assert_eq!(Solution::range_bitwise_and(0, 1), 0);
1723
}
1824
}

0 commit comments

Comments
 (0)