二刷 07/2022
Version #2 Turn off the right most bit of the larger number
Time O(1)
Space O(1)
Runtime: 7 ms, faster than 85.09% of Java online submissions for Bitwise AND of Numbers Range.
Memory Usage: 44.5 MB, less than 25.77% of Java online submissions for Bitwise AND of Numbers Range.
class Solution {
public int rangeBitwiseAnd(int left, int right) {
while (left < right) {
right = right & (right - 1);
}
return left & right;
}
}
Ref: https://www.cnblogs.com/grandyang/p/4431646.html
一刷
// Find the common prefix of m and n
100.00 %
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int bit = 0;
while ((m >> bit) != (n >> bit)) {
bit++;
}
return (m >> bit) << bit;
}
}
No comments:
Post a Comment