Skip to content

Commit 03cc97e

Browse files
refactor 201
1 parent a1435a2 commit 03cc97e

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

src/main/java/com/fishercoder/solutions/_201.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,24 @@
44
* 201. Bitwise AND of Numbers Range
55
*
66
* Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
7-
8-
For example, given the range [5, 7], you should return 4.
9-
7+
*
8+
* For example, given the range [5, 7], you should return 4.
109
*/
1110
public class _201 {
1211

13-
//this naive approach works, but will result in TLE as expected for 8256/8266 test cases: (0, 2147483647)
14-
public int rangeBitwiseAnd_TLE(int m, int n) {
15-
if (m == 0) {
16-
return m;
17-
}
18-
int result = m;
19-
for (int i = m + 1; i <= n; i++) {
20-
result &= i;
21-
}
22-
return result;
23-
}
24-
25-
//credit: https://discuss.leetcode.com/topic/28538/java-python-easy-solution-with-explanation
26-
//Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n
27-
//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
28-
//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
29-
public int rangeBitwiseAnd(int m, int n) {
30-
int i = 0;
31-
while (m != n) {
32-
m >>= 1;
33-
n >>= 1;
34-
i++;
12+
public static class Solution1 {
13+
//credit: https://discuss.leetcode.com/topic/28538/java-python-easy-solution-with-explanation
14+
//Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n
15+
//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
16+
//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
17+
public int rangeBitwiseAnd(int m, int n) {
18+
int i = 0;
19+
while (m != n) {
20+
m >>= 1;
21+
n >>= 1;
22+
i++;
23+
}
24+
return (n << i);
3525
}
36-
return (n << i);
3726
}
3827
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,58 @@
1010
* Created by fishercoder on 5/3/17.
1111
*/
1212
public class _201Test {
13-
private static _201 test;
13+
private static _201.Solution1 solution1;
1414
private static int m;
1515
private static int n;
1616
private static int actual;
1717
private static int expected;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _201();
21+
solution1 = new _201.Solution1();
2222
}
2323

2424
@Test
2525
public void test1() {
2626
m = 5;
2727
n = 7;
28-
actual = test.rangeBitwiseAnd_TLE(m, n);
28+
actual = solution1.rangeBitwiseAnd(m, n);
2929
expected = 4;
3030
assertEquals(expected, actual);
31-
actual = test.rangeBitwiseAnd(m, n);
31+
actual = solution1.rangeBitwiseAnd(m, n);
3232
assertEquals(expected, actual);
3333
}
3434

3535
@Test
3636
public void test2() {
3737
m = 1;
3838
n = 2;
39-
actual = test.rangeBitwiseAnd_TLE(m, n);
39+
actual = solution1.rangeBitwiseAnd(m, n);
4040
expected = 0;
4141
assertEquals(expected, actual);
42-
actual = test.rangeBitwiseAnd(m, n);
42+
actual = solution1.rangeBitwiseAnd(m, n);
4343
assertEquals(expected, actual);
4444
}
4545

4646
@Test
4747
public void test3() {
4848
m = 0;
4949
n = 2147483647;
50-
long start = System.currentTimeMillis();
51-
actual = test.rangeBitwiseAnd_TLE(m, n);
52-
System.out.println("It took " + (System.currentTimeMillis() - start) + " ms to finish.");
50+
actual = solution1.rangeBitwiseAnd(m, n);
5351
expected = 0;
5452
assertEquals(expected, actual);
55-
actual = test.rangeBitwiseAnd(m, n);
53+
actual = solution1.rangeBitwiseAnd(m, n);
5654
assertEquals(expected, actual);
5755
}
5856

5957
@Test
6058
public void test4() {
6159
m = 20000;
6260
n = 2147483647;
63-
long start = System.currentTimeMillis();
64-
// actual = test.rangeBitwiseAnd_TLE(m, n);
65-
System.out.println("It took " + (System.currentTimeMillis() - start) + " ms to finish.");
61+
actual = solution1.rangeBitwiseAnd(m, n);
6662
expected = 0;
6763
assertEquals(expected, actual);
68-
actual = test.rangeBitwiseAnd(m, n);
64+
actual = solution1.rangeBitwiseAnd(m, n);
6965
assertEquals(expected, actual);
7066
}
7167
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy