关闭

字符编码问题

242人阅读 评论(0) 收藏 举报
分类:
Given a byte array, which is an encoding of characters. Here is the rule:
    a. If the first bit of a byte is 0, that byte stands for a one-byte
character
    b. If the first bit of a byte is 1, that byte and its following byte
together stand for a two-byte character
Now implement a function to decide if the last character is a one-byte
character or a two-byte character
Constraint: You must scan the byte array from the end to the start.

Otherwise it will be very trivial.


bool isStart (vector<char> &A, int i) {
	if (i == 0 || (A[i - 1] >> 7) == 0) return true;
	return isStart(A, i - 1) == false;
}
int lastCharacterLen(vector<char> &A) {
	return isStart(A, A.size() - 1) ? 1 : 2;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:83624次
    • 积分:2964
    • 等级:
    • 排名:第11838名
    • 原创:225篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条