defgetBytes(num: int) -> int: if (num & MASK1) == 0: return1 n, mask = 0, MASK1 while num & mask: n += 1 if n > 4: return -1 mask >>= 1 return n if n >= 2else -1
index, m = 0, len(data) while index < m: n = getBytes(data[index]) if n < 0or index + n > m orany((ch & MASK2) != MASK1 for ch in data[index + 1: index + n]): returnFalse index += n returnTrue
publicbooleanvalidUtf8(int[] data) { intm= data.length; intindex=0; while (index < m) { intnum= data[index]; intn= getBytes(num); if (n < 0 || index + n > m) { returnfalse; } for (inti=1; i < n; i++) { if (!isValid(data[index + i])) { returnfalse; } } index += n; } returntrue; }
publicintgetBytes(int num) { if ((num & MASK1) == 0) { return1; } intn=0; intmask= MASK1; while ((num & mask) != 0) { n++; if (n > 4) { return -1; } mask >>= 1; } return n >= 2 ? n : -1; }
publicboolValidUtf8(int[] data) { int m = data.Length; int index = 0; while (index < m) { int num = data[index]; int n = GetBytes(num); if (n < 0 || index + n > m) { returnfalse; } for (int i = 1; i < n; i++) { if (!IsValid(data[index + i])) { returnfalse; } } index += n; } returntrue; }
publicintGetBytes(int num) { if ((num & MASK1) == 0) { return1; } int n = 0; int mask = MASK1; while ((num & mask) != 0) { n++; if (n > 4) { return-1; } mask >>= 1; } return n >= 2 ? n : -1; }
intgetBytes(int num){ if ((num & MASK1) == 0) { return1; } int n = 0; int mask = MASK1; while ((num & mask) != 0) { n++; if (n > 4) { return-1; } mask >>= 1; } return n >= 2 ? n : -1; }
boolvalidUtf8(vector<int>& data){ int m = data.size(); int index = 0; while (index < m) { int num = data[index]; int n = getBytes(num); if (n < 0 || index + n > m) { returnfalse; } for (int i = 1; i < n; i++) { if (!isValid(data[index + i])) { returnfalse; } } index += n; } returntrue; } };
intgetBytes(int num) { if ((num & MASK1) == 0) { return1; } int n = 0; int mask = MASK1; while ((num & mask) != 0) { n++; if (n > 4) { return-1; } mask >>= 1; } return n >= 2 ? n : -1; }
boolvalidUtf8(int* data, int dataSize){ int m = dataSize; int index = 0; while (index < m) { int num = data[index]; int n = getBytes(num); if (n < 0 || index + n > m) { returnfalse; } for (int i = 1; i < n; i++) { if (!isValid(data[index + i])) { returnfalse; } } index += n; } returntrue; }
var validUtf8 = function(data) { const m = data.length; let index = 0; while (index < m) { const num = data[index]; const n = getBytes(num); if (n < 0 || index + n > m) { returnfalse; } for (let i = 1; i < n; i++) { if (!isValid(data[index + i])) { returnfalse; } } index += n; } returntrue; };
constgetBytes = (num) => { if ((num & MASK1) === 0) { return1; } let n = 0; let mask = MASK1; while ((num & mask) !== 0) { n++; if (n > 4) { return -1; } mask >>= 1; } return n >= 2 ? n : -1; }
funcgetBytes(num int)int { if num&mask1 == 0 { return1 } n := 0 for mask := mask1; num&mask != 0; mask >>= 1 { n++ if n > 4 { return-1 } } if n >= 2 { return n } return-1 }
funcvalidUtf8(data []int)bool { for index, m := 0, len(data); index < m; { n := getBytes(data[index]) if n < 0 || index+n > m { returnfalse } for _, ch := range data[index+1 : index+n] { if ch&mask2 != mask1 { returnfalse } } index += n } returntrue }
复杂度分析
时间复杂度:$O(m)$,其中 $m$ 是数组 data 的长度。需要遍历数组 data 一次,对于数组中的每个元素的计算时间都是 $O(1)$。