Given an array of integers representing the data, return whether it is a valid utf-8 encoding.
Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.
Example 1:
1
2
3
4
data = [197, 130, 1], which represents the octet sequence: 110001011000001000000001.
Return true.
It is a valid utf-8 encoding fora2-bytescharacter followed bya1-bytecharacter.
Example 2:
1
2
3
4
5
6
data = [235, 140, 4], which represented the octet sequence: 111010111000110000000100.
Return false.
The first3 bits are all one's andthe4th bit is0 means itis a 3-bytes character.
The next byte is a continuation byte which starts with10andthat's correct.
But thesecond continuation byte doesnotstart with10, so itis invalid.