We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6843b57 commit 9e00e5dCopy full SHA for 9e00e5d
Medium/Bitwise AND of Numbers Range.java
@@ -1,34 +1,8 @@
1
class Solution {
2
- public int rangeBitwiseAnd(int m, int n) {
3
- int temp1 = m;
4
- int temp2 = n;
5
-
6
- int cnt1 = 0;
7
- int cnt2 = 0;
8
9
- while (temp1 > 1 || temp2 > 1) {
10
- if (temp1 > 1) {
11
- temp1 /= 2;
12
- cnt1++;
13
- }
14
15
- if (temp2 > 1) {
16
- temp2 /= 2;
17
- cnt2++;
18
19
20
21
- if (cnt1 != cnt2) {
22
- return 0;
23
24
25
- int res = ~0;
26
- for (int i=m;i<n;i++) {
27
- res = res&i;
28
29
30
- res = res&n;
31
32
- return res;
+ public int rangeBitwiseAnd(int left, int right) {
+ while (right > left) {
+ right = right & (right - 1);
33
}
+ return left & right;
+ }
34
0 commit comments