leetcode 393. UTF-8 Validation(UTF-8 编码验证)

Given an integer array data representing the data, return whether it is a valid UTF-8 encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

For a 1-byte character, the first bit is a 0, followed by its Unicode code.
For an n-bytes character, the first n bits are all one’s, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.
This is how the UTF-8 encoding would work:

     Number of Bytes   |        UTF-8 Octet Sequence
                       |              (binary)
   --------------------+-----------------------------------------
            1          |   0xxxxxxx
            2          |   110xxxxx 10xxxxxx
            3          |   1110xxxx 10xxxxxx 10xxxxxx
            4          |   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

x denotes a bit in the binary form of a byte that may be either 0 or 1.

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:

Input: data = [197,130,1]
Output: true
Explanation: data represents the octet sequence: 11000101 10000010 00000001.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
Example 2:

Input: data = [235,140,4]
Output: false
Explanation: data represented the octet sequence: 11101011 10001100 00000100.
The first 3 bits are all one’s and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that’s correct.
But the second continuation byte does not start with 10, so it is invalid.

看着很绕,简单解释下,
UFT-8是1~4byte长的,不能超过4byte.
4个byte可能出现的4种情况都在表里,不符合的就是false。
那你会说1 <= data.length <= 20000,data的长度可能到20000啊,怎么可能只有4 byte。
1~4 byte代表一个字符,后面的就是另一个字符,1~4 byte为一组。

思路:

经过上面的解释,可以做出如下分析。

第一个byte开头为0的不用考虑,肯定是UTF-8,跳过。
然后就是表里的case 2,3,4.
利用bit移位来判断,cnt记录后面应该出现多少个开头为10的byte.

public boolean validUtf8(int[] data) {
    int cnt = 0;
    
    for(int num : data) {
        if(cnt == 0) {
            if(num >> 5 == 0b110) cnt = 1;
            else if(num >> 4 == 0b1110) cnt = 2;
            else if(num >> 3 == 0b11110) cnt = 3;
            else if(num >> 7 == 1) return false;
        } else {
            if(num >> 6 != 0b10) return false;
            cnt --;
        }
    }
    
    return cnt == 0;
}

代码很简洁,但不是最快的,因为还要移位运算。
倒不如直接给出mask, 做与运算。
然后记录需要多少个“10”,一组一组判断。
注意numOfBytes中只有 numOfBytes-1个“10",比如4 byte中只有3个”10“

public boolean validUtf8(int[] data) {
    boolean isValid = true;
	for(int i=0;i<data.length;i++) {
		if(data[i]>255) return false; // 1 after 8th digit, 100000000
		int numberOfBytes = 0;
		if((data[i] & 128) == 0) numberOfBytes = 1; // 0xxxxxxx, 1 byte, 128(10000000)
		else if((data[i] & 224) == 192) numberOfBytes = 2; // 110xxxxx, 2 bytes, 224(11100000), 192(11000000)
		else if((data[i] & 240) == 224) numberOfBytes = 3; // 1110xxxx, 3 bytes, 240(11110000), 224(11100000)
		else if((data[i] & 248) == 240) numberOfBytes = 4; // 11110xxx, 4 bytes, 248(11111000), 240(11110000)
		else return false;

		for(int j=1;j<numberOfBytes;j++) { // check that the next n bytes start with 10xxxxxx
			if(i+j>=data.length) return false;
			if((data[i+j] & 192) != 128) return false; // 192(11000000), 128(10000000)
		}
		i=i+numberOfBytes-1; //循环结束后还会i++
	}
	return isValid;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值