leetcode393. UTF-8 Validation

weixin_33766168發表於2019-02-17

題目要求

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

For 1-byte character, the first bit is a 0, followed by its unicode code.
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.

檢驗整數陣列能否構成合法的UTF8編碼的序列。UTF8的位元組編碼規則如下:

  1. 每個UTF8字元包含1~4個位元組
  2. 如果只包含1個位元組,則該位元組以0作為開頭,剩下的位隨意
  3. 如果包含兩個或兩個以上位元組,則起始位元組以n個1和1個0開頭,例如,如果該UTF8字元包含兩個位元組,則第一個位元組以110開頭,同理,三個字元的第一個位元組以1110開頭。剩餘的位元組必須以10開頭。

思路和程式碼

首先我們整理一下,每一種型別的UTF8字元包含什麼樣的規格:

  1. 只包含一個位元組,該位元組格式為0xxxxxxx,則轉換為整數的話,該整數必須小於128(1000000)
  2. 包含多個位元組,則頭位元組格式為110xxxxx, 1110xxxx, 11110xxx。而緊跟其後的字元必須格式為10xxxxxx。

綜上所述:

  1. num<1000000: 單位元組
  2. 10000000=<num<11000000: 多位元組字元的跟隨位元組
  3. 11000000<=num<11100000: 兩個位元組的起始位元組
  4. 11100000<=num<11110000: 三個位元組的起始位元組
  5. 11110000<=num<11111000: 四個位元組的起始位元組

下面分別是這題的兩種實現:

遞迴實現:

    private static final int ONE_BYTE = 128; //10000000
    private static final int FOLLOW_BYTE = 192; //11000000
    private static final int TWO_BYTE = 224; //11100000
    private static final int THREE_BYTE = 240;//11110000
    private static final int FOUR_BYTE = 248;//11111000
    public boolean validUtf8(int[] data) {
        return validUtf8(data, 0);
    }
    
    public boolean validUtf8(int[] data, int startAt) {
        if(startAt >= data.length) return true;
        int first = data[startAt];
        
        int followLength = 0;
        if(first < ONE_BYTE) {
            return validUtf8(data, startAt+1);
        }else if(first < FOLLOW_BYTE){
            return false;
        }else if(first <TWO_BYTE) {
            followLength = 2;
        }else if(first < THREE_BYTE) {
            followLength = 3;
        }else if(first < FOUR_BYTE) {
            followLength = 4;
        }else {
            return false;
        }
        if(startAt + followLength > data.length) return false; 
        for(int i = 1 ; i<followLength ; i++) {
            int next = data[startAt + i];
            if(next < ONE_BYTE || next >= FOLLOW_BYTE) {
                return false;
            }
        }
        return validUtf8(data, startAt + followLength);
    }

迴圈實現:

    private static final int ONE_BYTE = 128; //10000000
    private static final int FOLLOW_BYTE = 192; //11000000
    private static final int TWO_BYTE = 224; //11100000
    private static final int THREE_BYTE = 240;//11110000
    private static final int FOUR_BYTE = 248;//11111000
    public boolean validUtf8(int[] data) {
        return validUtf8(data, 0);
    }
    
    public boolean validUtf8(int[] data, int startAt) {
        int followCount = 0;
        for(int num : data) {
            if(num < ONE_BYTE) {
                if(followCount != 0) {
                    return false;
                }
            }else if(num < FOLLOW_BYTE) {
                if(followCount == 0) {
                    return false;
                }
                followCount--;
            }else if(num < TWO_BYTE) {
                if(followCount != 0) {
                    return false;
                }
                followCount = 1;
            }else if(num < THREE_BYTE) {
                if(followCount != 0) {
                    return false;
                }
                followCount = 2;
            }else if(num < FOUR_BYTE) {
                if(followCount != 0) {
                    return false;
                }
                followCount = 3;
            }else {
                return false;
            }
        }
        return followCount == 0;
    }

相關文章