forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_393.java
34 lines (31 loc) · 984 Bytes
/
_393.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
public class _393 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4
*/
public boolean validUtf8(int[] data) {
int count = 0;
for (int d : data) {
if (count == 0) {
if ((d >> 5) == 0b110) {
count = 1;
} else if ((d >> 4) == 0b1110) {
count = 2;
} else if ((d >> 3) == 0b11110) {
count = 3;
} else if ((d >> 7) == 1) {
return false;
}
} else {
if ((d >> 6) != 0b10) {
return false;
} else {
count--;
}
}
}
return count == 0;
}
}
}