393 UTF-8 Validation UTF-8 编码验证

详见:https://leetcode.com/problems/utf-8-validation/description/

C++:

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        int cnt = 0;
        for (int d : data) 
        {
            if (cnt == 0) 
            {
                if ((d >> 5) == 0b110) 
                {
                    cnt = 1;
                }
                else if ((d >> 4) == 0b1110) 
                {
                    cnt = 2;
                }
                else if ((d >> 3) == 0b11110) 
                {
                    cnt = 3;
                }
                else if (d >> 7)
                {
                    return false;
                }
            } else {
                if ((d >> 6) != 0b10) 
                {
                    return false;
                }
                --cnt;
            }
        }
        return cnt == 0;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5847597.html

posted on 2018-04-15 22:14  lina2014  阅读(170)  评论(0编辑  收藏  举报

导航