摘要:題目要求檢驗整數(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): 循環(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) {
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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/73285.html
摘要:題目鏈接這道題關(guān)鍵是搞懂題目意思。思路及代碼知道意思之后,這道題就很簡單了。一個,每次分三步來做,是每次都是新的統(tǒng)計后位里面,從前開始有多少個,用變量來保存,其中可能的值只有從開始檢查,后八位中的前兩位是否為,一共檢查更新的值為 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...
摘要:時間年月日星期三說明使用規(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接口...
摘要:和上標注的約束都會被執(zhí)行注意如果子類覆蓋了父類的方法,那么子類和父類的約束都會被校驗。 每篇一句 沒有任何技術(shù)方案會是一種銀彈,任何東西都是有利弊的 相關(guān)閱讀 【小家Java】深入了解數(shù)據(jù)校驗:Java Bean Validation 2.0(JSR303、JSR349、JSR380)Hibernate-Validation 6.x使用案例【小家Spring】Spring方法級別數(shù)據(jù)校...
閱讀 1148·2021-09-22 15:43
閱讀 2345·2021-09-22 15:32
閱讀 4455·2021-09-22 15:11
閱讀 2188·2019-08-30 15:55
閱讀 2564·2019-08-30 15:54
閱讀 984·2019-08-30 15:44
閱讀 1095·2019-08-29 13:26
閱讀 794·2019-08-29 12:54