Skip to content

Commit a32fa1b

Browse files
refactor 393
1 parent a9e5df5 commit a32fa1b

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,31 @@
3838
*/
3939
public class _393 {
4040

41-
/**credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4*/
42-
public boolean validUtf8(int[] data) {
43-
int count = 0;
44-
for (int d : data) {
45-
if (count == 0) {
46-
if ((d >> 5) == 0b110) {
47-
count = 1;
48-
} else if ((d >> 4) == 0b1110) {
49-
count = 2;
50-
} else if ((d >> 3) == 0b11110) {
51-
count = 3;
52-
} else if ((d >> 7) == 1) {
53-
return false;
54-
}
55-
} else {
56-
if ((d >> 6) != 0b10) {
57-
return false;
41+
public static class Solution1 {
42+
/** credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4 */
43+
public boolean validUtf8(int[] data) {
44+
int count = 0;
45+
for (int d : data) {
46+
if (count == 0) {
47+
if ((d >> 5) == 0b110) {
48+
count = 1;
49+
} else if ((d >> 4) == 0b1110) {
50+
count = 2;
51+
} else if ((d >> 3) == 0b11110) {
52+
count = 3;
53+
} else if ((d >> 7) == 1) {
54+
return false;
55+
}
5856
} else {
59-
count--;
57+
if ((d >> 6) != 0b10) {
58+
return false;
59+
} else {
60+
count--;
61+
}
6062
}
6163
}
64+
return count == 0;
6265
}
63-
return count == 0;
6466
}
6567

6668
}

src/test/java/com/fishercoder/_393Test.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,31 @@
77

88
import static junit.framework.Assert.assertEquals;
99

10-
/**
11-
* Created by fishercoder on 5/3/17.
12-
*/
1310
public class _393Test {
14-
private static _393 test;
15-
private static boolean expected;
16-
private static boolean actual;
17-
private static int[] data;
11+
private static _393.Solution1 solution1;
12+
private static boolean expected;
13+
private static boolean actual;
14+
private static int[] data;
1815

19-
@BeforeClass
20-
public static void setup() {
21-
test = new _393();
22-
}
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _393.Solution1();
19+
}
2320

24-
@Test
25-
@Ignore
26-
public void test1() {
27-
data = new int[]{197, 130, 1};
28-
expected = true;
29-
actual = test.validUtf8(data);
30-
assertEquals(expected, actual);
31-
}
21+
@Test
22+
@Ignore
23+
public void test1() {
24+
data = new int[] {197, 130, 1};
25+
expected = true;
26+
actual = solution1.validUtf8(data);
27+
assertEquals(expected, actual);
28+
}
3229

33-
@Test
34-
public void test2() {
35-
data = new int[]{5};
36-
expected = true;
37-
actual = test.validUtf8(data);
38-
assertEquals(expected, actual);
39-
}
30+
@Test
31+
public void test2() {
32+
data = new int[] {5};
33+
expected = true;
34+
actual = solution1.validUtf8(data);
35+
assertEquals(expected, actual);
36+
}
4037
}

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