Skip to content

Commit d5335de

Browse files
refactor 201
1 parent 7606fbd commit d5335de

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 201. Bitwise AND of Numbers Range
5-
*
6-
* Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
7-
*
8-
* Example 1:
9-
* Input: [5,7]
10-
* Output: 4
11-
*
12-
* Example 2:
13-
* Input: [0,1]
14-
* Output: 0
15-
*
16-
*/
173
public class _201 {
184

195
public static class Solution1 {
@@ -22,15 +8,17 @@ public static class Solution1 {
228
* Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n
239
* e.g. 5 and 7, in binary, they are 101 and 111, the result for [5,7] is 5&6&7 which is 101&110&111
2410
* this we can understand it to be shifting the digits of m and n from left to right until they become the same, then we pad that number with zeroes on the right side
11+
* <p>
12+
* A more visual explanation here: https://leetcode.com/problems/bitwise-and-of-numbers-range/solution/
2513
*/
26-
public int rangeBitwiseAnd(int m, int n) {
27-
int i = 0;
28-
while (m != n) {
29-
m >>= 1;
30-
n >>= 1;
31-
i++;
14+
public int rangeBitwiseAnd(int left, int right) {
15+
int shift = 0;
16+
while (left != right) {
17+
left >>= 1;
18+
right >>= 1;
19+
shift++;
3220
}
33-
return (n << i);
21+
return right << shift;
3422
}
3523
}
3624
}

src/test/java/com/fishercoder/_201Test.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/3/17.
11-
*/
129
public class _201Test {
1310
private static _201.Solution1 solution1;
14-
private static int m;
15-
private static int n;
11+
private static int left;
12+
private static int right;
1613
private static int actual;
1714
private static int expected;
1815

@@ -23,45 +20,45 @@ public static void setup() {
2320

2421
@Test
2522
public void test1() {
26-
m = 5;
27-
n = 7;
28-
actual = solution1.rangeBitwiseAnd(m, n);
23+
left = 5;
24+
right = 7;
25+
actual = solution1.rangeBitwiseAnd(left, right);
2926
expected = 4;
3027
assertEquals(expected, actual);
31-
actual = solution1.rangeBitwiseAnd(m, n);
28+
actual = solution1.rangeBitwiseAnd(left, right);
3229
assertEquals(expected, actual);
3330
}
3431

3532
@Test
3633
public void test2() {
37-
m = 1;
38-
n = 2;
39-
actual = solution1.rangeBitwiseAnd(m, n);
34+
left = 1;
35+
right = 2;
36+
actual = solution1.rangeBitwiseAnd(left, right);
4037
expected = 0;
4138
assertEquals(expected, actual);
42-
actual = solution1.rangeBitwiseAnd(m, n);
39+
actual = solution1.rangeBitwiseAnd(left, right);
4340
assertEquals(expected, actual);
4441
}
4542

4643
@Test
4744
public void test3() {
48-
m = 0;
49-
n = 2147483647;
50-
actual = solution1.rangeBitwiseAnd(m, n);
45+
left = 0;
46+
right = 2147483647;
47+
actual = solution1.rangeBitwiseAnd(left, right);
5148
expected = 0;
5249
assertEquals(expected, actual);
53-
actual = solution1.rangeBitwiseAnd(m, n);
50+
actual = solution1.rangeBitwiseAnd(left, right);
5451
assertEquals(expected, actual);
5552
}
5653

5754
@Test
5855
public void test4() {
59-
m = 20000;
60-
n = 2147483647;
61-
actual = solution1.rangeBitwiseAnd(m, n);
56+
left = 20000;
57+
right = 2147483647;
58+
actual = solution1.rangeBitwiseAnd(left, right);
6259
expected = 0;
6360
assertEquals(expected, actual);
64-
actual = solution1.rangeBitwiseAnd(m, n);
61+
actual = solution1.rangeBitwiseAnd(left, right);
6562
assertEquals(expected, actual);
6663
}
6764
}

0 commit comments

Comments
 (0)