国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

leetcode393. UTF-8 Validation

Cruise_Chan / 2465人閱讀

摘要:題目要求檢驗整數(shù)數(shù)組能否構(gòu)成合法的編碼的序列。剩余的字節(jié)必須以開頭。而緊跟其后的字符必須格式為。綜上所述單字節(jié)多字節(jié)字符的跟隨字節(jié)兩個字節(jié)的起始字節(jié)三個字節(jié)的起始字節(jié)四個字節(jié)的起始字節(jié)下面分別是這題的兩種實現(xiàn)遞歸實現(xiàn)循環(huán)實現(xiàn)

題目要求
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.

檢驗整數(shù)數(shù)組能否構(gòu)成合法的UTF8編碼的序列。UTF8的字節(jié)編碼規(guī)則如下:

每個UTF8字符包含1~4個字節(jié)

如果只包含1個字節(jié),則該字節(jié)以0作為開頭,剩下的位隨意

如果包含兩個或兩個以上字節(jié),則起始字節(jié)以n個1和1個0開頭,例如,如果該UTF8字符包含兩個字節(jié),則第一個字節(jié)以110開頭,同理,三個字符的第一個字節(jié)以1110開頭。剩余的字節(jié)必須以10開頭。

思路和代碼

首先我們整理一下,每一種類型的UTF8字符包含什么樣的規(guī)格:

只包含一個字節(jié),該字節(jié)格式為0xxxxxxx,則轉(zhuǎn)換為整數(shù)的話,該整數(shù)必須小于128(1000000)

包含多個字節(jié),則頭字節(jié)格式為110xxxxx, 1110xxxx, 11110xxx。而緊跟其后的字符必須格式為10xxxxxx。

綜上所述:

num<1000000: 單字節(jié)

10000000=

11000000<=num<11100000: 兩個字節(jié)的起始字節(jié)

11100000<=num<11110000: 三個字節(jié)的起始字節(jié)

11110000<=num<11111000: 四個字節(jié)的起始字節(jié)

下面分別是這題的兩種實現(xiàn):

遞歸實現(xiàn):

    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  data.length) return false; 
        for(int i = 1 ; i= FOLLOW_BYTE) {
                return false;
            }
        }
        return validUtf8(data, startAt + followLength);
    }

循環(huán)實現(xiàn):

    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;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/73285.html

相關(guān)文章

  • UTF-8 Validation

    摘要:題目鏈接這道題關(guān)鍵是搞懂題目意思。思路及代碼知道意思之后,這道題就很簡單了。一個,每次分三步來做,是每次都是新的統(tǒng)計后位里面,從前開始有多少個,用變量來保存,其中可能的值只有從開始檢查,后八位中的前兩位是否為,一共檢查更新的值為 UTF-8 Validation 題目鏈接:https://leetcode.com/problems... 這道題關(guān)鍵是搞懂題目意思。 UTF-8 1 by...

    Kahn 評論0 收藏0
  • [LintCode] UTF-8 Validation

    Problem 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...

    tolerious 評論0 收藏0
  • 記錄_使用JSR303規(guī)范進行數(shù)據(jù)校驗

    摘要:時間年月日星期三說明使用規(guī)范校驗接口請求參數(shù)源碼第一章理論簡介背景介紹如今互聯(lián)網(wǎng)項目都采用接口形式進行開發(fā)。該規(guī)范定義了一個元數(shù)據(jù)模型,默認的元數(shù)據(jù)來源是注解。 時間:2017年11月08日星期三說明:使用JSR303規(guī)范校驗http接口請求參數(shù) 源碼:https://github.com/zccodere/s... 第一章:理論簡介 1-1 背景介紹 如今互聯(lián)網(wǎng)項目都采用HTTP接口...

    187J3X1 評論0 收藏0
  • 容器最大盛水量

    摘要:容器最大盛水量給定個非負整數(shù),,,,其中每個表示坐標,處的點。找到兩條線,它們與軸一起形成一個容器,使得容器含有最多的水。 容器最大盛水量 Container With Most Water 給定n個非負整數(shù)a1,a2,...,an,其中每個表示坐標(i,ai)處的點。 繪制n條垂直線,使得線i的兩個端點在(i,ai)和(i,0)處。 找到兩條線,它們與x軸一起形成一個容器,使得容器...

    luckyw 評論0 收藏0
  • Bean Validation完結(jié)篇:你必須關(guān)注的邊邊角角(約束級聯(lián)、自定義約束、自定義校驗器、國際

    摘要:和上標注的約束都會被執(zhí)行注意如果子類覆蓋了父類的方法,那么子類和父類的約束都會被校驗。 每篇一句 沒有任何技術(shù)方案會是一種銀彈,任何東西都是有利弊的 相關(guān)閱讀 【小家Java】深入了解數(shù)據(jù)校驗:Java Bean Validation 2.0(JSR303、JSR349、JSR380)Hibernate-Validation 6.x使用案例【小家Spring】Spring方法級別數(shù)據(jù)校...

    niuxiaowei111 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<