LeetCode - Algorithms - 393. UTF-8 Validation

Just verify code of others.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public boolean validUtf8(int[] data) {
int count=0;
for(int k : data) {
if (count==0) {
if ((k>>5)==0b110) count=1;
else if ((k>>4)==0b1110) count=2;
else if ((k>>3)==0b11110) count=3;
else if ((k>>7)==1) return false;
}
else {
if ((k>>6)!=0b10) return false;
count--;
}
}
return count==0;
}
}

binary (introduced in Java SE 7) 0b11110101 (0b followed by a binary number)

Submission Detail

  • 49 / 49 test cases passed.
  • Runtime: 6 ms
  • Your runtime beats 27.65 % of java submissions.

ref