Skip to content

Commit 10c083b

Browse files
add 201
1 parent 9ed0bd9 commit 10c083b

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ Your ideas/fixes/algorithms are more than welcome!
258258
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[Solution](../../blmaster/src/94stevesun/algorithms/IsomorphicStrings.java)| O(n)|O(1) | Easy
259259
|204|[Count Primes](https://leetcode.com/problems/count-primes/)|[Solution](../master/src/main/java/com/stevesun/solutions/CountPrime.java)| O(?)|O(?) | Easy
260260
|202|[Happy Number](https://leetcode.com/problems/happy-number/)|[Solution](../master/src/main/java/com/stevesun/solutions/HappyNumber.java)| O(k)|O(k) | Easy
261+
|201|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/)|[Solution](../master/src/main/java/com/stevesun/solutions/_201.java)| O(min(m,n))|O(1) | Medium | Bit Manipulation
261262
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../master/src/main/java/com/stevesun/solutions/NumberOfIslandsUnionFind.java) [DFS](../../blmaster/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
262263
|198|[House Robber](https://leetcode.com/problems/house-robber/)|[Solution](../master/src/main/java/com/stevesun/solutions/HouseRobber.java)| O(n)|O(n)| Easy | DP
263264
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/)|[Solution](../master/src/main/java/com/stevesun/solutions/ReverseBits.java)| O(n)|O(1)| Easy | Bit Manipulation
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.stevesun.solutions;
2+
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+
For example, given the range [5, 7], you should return 4.
9+
10+
*/
11+
public class _201 {
12+
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) return m;
16+
int result = m;
17+
for (int i = m+1; i <= n; i++) {
18+
result &= i;
19+
}
20+
return result;
21+
}
22+
23+
//credit: https://discuss.leetcode.com/topic/28538/java-python-easy-solution-with-explanation
24+
//Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n
25+
//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
26+
//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
27+
public int rangeBitwiseAnd(int m, int n) {
28+
int i = 0;
29+
while (m != n) {
30+
m >>= 1;
31+
n >>= 1;
32+
i++;
33+
}
34+
return (n << i);
35+
}
36+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._201;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 5/3/17.
11+
*/
12+
public class _201Test {
13+
private static _201 test;
14+
private static int m;
15+
private static int n;
16+
private static int actual;
17+
private static int expected;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new _201();
22+
}
23+
24+
@Test
25+
public void test1(){
26+
m = 5;
27+
n = 7;
28+
actual = test.rangeBitwiseAnd_TLE(m, n);
29+
expected = 4;
30+
assertEquals(expected, actual);
31+
actual = test.rangeBitwiseAnd(m, n);
32+
assertEquals(expected, actual);
33+
}
34+
35+
@Test
36+
public void test2(){
37+
m = 1;
38+
n = 2;
39+
actual = test.rangeBitwiseAnd_TLE(m, n);
40+
expected = 0;
41+
assertEquals(expected, actual);
42+
actual = test.rangeBitwiseAnd(m, n);
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Test
47+
public void test3(){
48+
m = 0;
49+
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.");
53+
expected = 0;
54+
assertEquals(expected, actual);
55+
actual = test.rangeBitwiseAnd(m, n);
56+
assertEquals(expected, actual);
57+
}
58+
59+
@Test
60+
public void test4(){
61+
m = 20000;
62+
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.");
66+
expected = 0;
67+
assertEquals(expected, actual);
68+
actual = test.rangeBitwiseAnd(m, n);
69+
assertEquals(expected, actual);
70+
}
71+
}

0 commit comments

Comments
 (0)