Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 393. UTF-8 Validation #393

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 393. UTF-8 Validation #393

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

 

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

  1. For 1-byte character, the first bit is a 0, followed by its unicode code.
  2. For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.

This is how the UTF-8 encoding would work:

   Char. number range  |        UTF-8 octet sequence
      (hexadecimal)    |              (binary)
   --------------------+---------------------------------------------
   0000 0000-0000 007F | 0xxxxxxx
   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

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:

data = [197, 130, 1], which represents the octet sequence: **11000101 10000010 00000001**.

Return **true**.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.

Example 2:

data = [235, 140, 4], which represented the octet sequence: **11101011 10001100 00000100**.+

Return **false**.
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.

 

这道题考察我们 UTF-8 编码,这种互联网所采用的通用的编码格式的产生是为了解决ASCII只能表示英文字符的局限性,和统一 Unicode 的实现方式。下面这段摘自维基百科 UTF-8 编码

对于 UTF-8 编码中的任意字节B,如果B的第一位为0,则B独立的表示一个字符(ASCII 码);
如果B的第一位为1,第二位为0,则B为一个多字节字符中的一个字节(非 ASCII 字符);
如果B的前两位为1,第三位为0,则B为两个字节表示的字符中的第一个字节;
如果B的前三位为1,第四位为0,则B为三个字节表示的字符中的第一个字节;
如果B的前四位为1,第五位为0,则B为四个字节表示的字符中的第一个字节;
因此,对 UTF-8 编码中的任意字节,根据第一位,可判断是否为 ASCII 字符;根据前二位,可判断该字节是否为一个字符编码的第一个字节;根据前四位(如果前两位均为1),可确定该字节为字符编码的第一个字节,并且可判断对应的字符由几个字节表示;根据前五位(如果前四位为1),可判断编码是否有错误或数据传输过程中是否有错误。

那么根据上面的描述,我们可以先来判断第一位,如果是0的话,则说明是 ASCII 码,我们直接跳过,判断方法是只要比二进制数 10000000 小的数第一位肯定是0,然后我们来处理第一位是1的情况,由于第一位的1只是个标识符,后面连续跟的1的个数才是表示后面的字节的个数,我们可以统一从第一位开始连续1的个数,然后减去1就是后面的字节的个数,我想的办法是如果该数字大于等于 128,则表示第一位是1,然后减去 128,如果得到的数大于等于 64,则表示第二位是1,依次类推就可以得到连续的个数 cnt,我们要注意 10000000 这个数是不合法的,所以当 cnt 为1的时候直接返回 false,还有就是连续1的个数不能超过4个,当 cnt 大于4时也是不合法的。即便是当 cnt 为 [2, 4] 之间的数,若后面没有跟正确个数的字节,还是非法的,所以当 cnt > n-i 时还是 false。我们得到了合法的 cnt 的个数,只要验证后面的字节是否是以 10 开头的数即可,验证方法也很简单,只要这个数在 10000000 ~ 10111111 范围之间,则一定是 10 开头的,参见代码如下:

 

解法一:

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        int n = data.size();
        for (int i = 0; i < n; ++i) {
            if (data[i] < 0b10000000) {
                continue;
            } else {
                int cnt = 0, val = data[i];
                for (int j = 7; j >= 1; --j) {
                    if (val >= pow(2, j)) ++cnt;
                    else break;
                    val -= pow(2, j);
                }
                if (cnt == 1 || cnt > 4 || cnt > n - i) return false;
                for (int j = i + 1; j < i + cnt; ++j) {
                    if (data[j] > 0b10111111 || data[j] < 0b10000000) return false;
                } 
                i += cnt - 1;
            }
        }
        return true;
    }
};

 

在论坛里看到了一种非常简洁的方法,大神就是大神啊,这种方法也是要记连续1的个数,如果是标识字节,先将其向右平移五位,如果得到 110,则说明后面跟了一个字节,否则向右平移四位,如果得到 1110,则说明后面跟了两个字节,否则向右平移三位,如果得到 11110,则说明后面跟了三个字节,否则向右平移七位,如果为1的话,说明是 10000000 这种情况,不能当标识字节,直接返回 false。在非标识字节中,向右平移六位,如果得到的不是 10,则说明不是以 10 开头的,直接返回 false,否则 cnt 自减1,成功完成遍历返回 true,参见代码如下:

 

解法二:

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        int cnt = 0;
        for (int d : data) {
            if (cnt == 0) {
                if ((d >> 5) == 0b110) cnt = 1;
                else if ((d >> 4) == 0b1110) cnt = 2;
                else if ((d >> 3) == 0b11110) cnt = 3;
                else if (d >> 7) return false;
            } else {
                if ((d >> 6) != 0b10) return false;
                --cnt;
            }
        }
        return cnt == 0;
    }
};

 

Github 同步地址:

#393

 

参考资料:

https://leetcode.com/problems/utf-8-validation/

https://leetcode.com/problems/utf-8-validation/discuss/87464/Bit-Manipulation-Java-6ms

https://leetcode.com/problems/utf-8-validation/discuss/87462/Concise-C%2B%2B-implementation

 

LeetCode All in One 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant