g0301_0400.s0393_utf_8_validation.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0393_utf_8_validation;
// #Medium #Array #Bit_Manipulation #2022_07_13_Time_1_ms_(100.00%)_Space_43_MB_(87.62%)
public class Solution {
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;
}
}