摘要:題目要求檢驗(yàn)整數(shù)數(shù)組能否構(gòu)成合法的編碼的序列。剩余的字節(jié)必須以開(kāi)頭。而緊跟其后的字符必須格式為。綜上所述單字節(jié)多字節(jié)字符的跟隨字節(jié)兩個(gè)字節(jié)的起始字節(jié)三個(gè)字節(jié)的起始字節(jié)四個(gè)字節(jié)的起始字節(jié)下面分別是這題的兩種實(shí)現(xiàn)遞歸實(shí)現(xiàn)循環(huán)實(shí)現(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.
檢驗(yàn)整數(shù)數(shù)組能否構(gòu)成合法的UTF8編碼的序列。UTF8的字節(jié)編碼規(guī)則如下:
每個(gè)UTF8字符包含1~4個(gè)字節(jié)
如果只包含1個(gè)字節(jié),則該字節(jié)以0作為開(kāi)頭,剩下的位隨意
如果包含兩個(gè)或兩個(gè)以上字節(jié),則起始字節(jié)以n個(gè)1和1個(gè)0開(kāi)頭,例如,如果該UTF8字符包含兩個(gè)字節(jié),則第一個(gè)字節(jié)以110開(kāi)頭,同理,三個(gè)字符的第一個(gè)字節(jié)以1110開(kāi)頭。剩余的字節(jié)必須以10開(kāi)頭。
思路和代碼首先我們整理一下,每一種類(lèi)型的UTF8字符包含什么樣的規(guī)格:
只包含一個(gè)字節(jié),該字節(jié)格式為0xxxxxxx,則轉(zhuǎn)換為整數(shù)的話(huà),該整數(shù)必須小于128(1000000)
包含多個(gè)字節(jié),則頭字節(jié)格式為110xxxxx, 1110xxxx, 11110xxx。而緊跟其后的字符必須格式為10xxxxxx。
綜上所述:
num<1000000: 單字節(jié)
10000000= 11000000<=num<11100000: 兩個(gè)字節(jié)的起始字節(jié) 11100000<=num<11110000: 三個(gè)字節(jié)的起始字節(jié) 11110000<=num<11111000: 四個(gè)字節(jié)的起始字節(jié) 下面分別是這題的兩種實(shí)現(xiàn): 遞歸實(shí)現(xiàn): 循環(huán)實(shí)現(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
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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/73285.html
摘要:題目鏈接這道題關(guān)鍵是搞懂題目意思。思路及代碼知道意思之后,這道題就很簡(jiǎn)單了。一個(gè),每次分三步來(lái)做,是每次都是新的統(tǒng)計(jì)后位里面,從前開(kāi)始有多少個(gè),用變量來(lái)保存,其中可能的值只有從開(kāi)始檢查,后八位中的前兩位是否為,一共檢查更新的值為 UTF-8 Validation 題目鏈接:https://leetcode.com/problems... 這道題關(guān)鍵是搞懂題目意思。 UTF-8 1 by...
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...
摘要:時(shí)間年月日星期三說(shuō)明使用規(guī)范校驗(yàn)接口請(qǐng)求參數(shù)源碼第一章理論簡(jiǎn)介背景介紹如今互聯(lián)網(wǎng)項(xiàng)目都采用接口形式進(jìn)行開(kāi)發(fā)。該規(guī)范定義了一個(gè)元數(shù)據(jù)模型,默認(rèn)的元數(shù)據(jù)來(lái)源是注解。 時(shí)間:2017年11月08日星期三說(shuō)明:使用JSR303規(guī)范校驗(yàn)http接口請(qǐng)求參數(shù) 源碼:https://github.com/zccodere/s... 第一章:理論簡(jiǎn)介 1-1 背景介紹 如今互聯(lián)網(wǎng)項(xiàng)目都采用HTTP接口...
摘要:容器最大盛水量給定個(gè)非負(fù)整數(shù),,,,其中每個(gè)表示坐標(biāo),處的點(diǎn)。找到兩條線(xiàn),它們與軸一起形成一個(gè)容器,使得容器含有最多的水。 容器最大盛水量 Container With Most Water 給定n個(gè)非負(fù)整數(shù)a1,a2,...,an,其中每個(gè)表示坐標(biāo)(i,ai)處的點(diǎn)。 繪制n條垂直線(xiàn),使得線(xiàn)i的兩個(gè)端點(diǎn)在(i,ai)和(i,0)處。 找到兩條線(xiàn),它們與x軸一起形成一個(gè)容器,使得容器...
摘要:和上標(biāo)注的約束都會(huì)被執(zhí)行注意如果子類(lèi)覆蓋了父類(lèi)的方法,那么子類(lèi)和父類(lèi)的約束都會(huì)被校驗(yàn)。 每篇一句 沒(méi)有任何技術(shù)方案會(huì)是一種銀彈,任何東西都是有利弊的 相關(guān)閱讀 【小家Java】深入了解數(shù)據(jù)校驗(yàn):Java Bean Validation 2.0(JSR303、JSR349、JSR380)Hibernate-Validation 6.x使用案例【小家Spring】Spring方法級(jí)別數(shù)據(jù)校...
閱讀 1162·2021-09-22 15:43
閱讀 2358·2021-09-22 15:32
閱讀 4530·2021-09-22 15:11
閱讀 2227·2019-08-30 15:55
閱讀 2598·2019-08-30 15:54
閱讀 995·2019-08-30 15:44
閱讀 1107·2019-08-29 13:26
閱讀 803·2019-08-29 12:54