Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5335de

Browse files
refactor 201
1 parent7606fbd commitd5335de

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
packagecom.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
publicclass_201 {
184

195
publicstaticclassSolution1 {
@@ -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-
publicintrangeBitwiseAnd(intm,intn) {
27-
inti =0;
28-
while (m !=n) {
29-
m >>=1;
30-
n >>=1;
31-
i++;
14+
publicintrangeBitwiseAnd(intleft,intright) {
15+
intshift =0;
16+
while (left !=right) {
17+
left >>=1;
18+
right >>=1;
19+
shift++;
3220
}
33-
return(n <<i);
21+
returnright <<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
importstaticorg.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/3/17.
11-
*/
129
publicclass_201Test {
1310
privatestatic_201.Solution1solution1;
14-
privatestaticintm;
15-
privatestaticintn;
11+
privatestaticintleft;
12+
privatestaticintright;
1613
privatestaticintactual;
1714
privatestaticintexpected;
1815

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

2421
@Test
2522
publicvoidtest1() {
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
publicvoidtest2() {
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
publicvoidtest3() {
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
publicvoidtest4() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp