utf-8-validation

时间:2023-03-09 23:00:54
utf-8-validation
https://leetcode.com/problems/utf-8-validation/

public class Solution {
public boolean validUtf8(int[] data) {
int last = 0;
for (int i=0; i<data.length; i++) {
String str = Integer.toBinaryString(data[i]);
if (last > 0) {
if (str.length() != 8 || !str.startsWith("10")) {
return false;
}
last--;
}
else {
if (str.length() > 8) {
return false;
}
if (str.length() == 8) {
int j=0;
for (; j<8; j++) {
if (str.charAt(j) != '1') {
break;
}
}
if (j > 1 && j <= 4) {
last = j - 1;
}
else {
return false;
}
}
}
}
if (last == 0) {
return true;
}
else {
return false;
}
}
}