393. UTF-8 Validation

class Solution {
public:
    bool check(int st, int count, vector<int>& data) {
        if (st + count > data.size()) return false;
        for (int i = st + 1; i < st + count; i++)
            if (data[i] >> 6 != 0b10) return false;
        return true;
    }
    bool validUtf8(vector<int>& data) {
        for (int i = 0; i < data.size(); ) {
            if (data[i] >> 7 == 0) i += 1;
                else if (data[i] >> 5 == 0b110 && check(i, 2, data)) i += 2;
                    else if (data[i] >> 4 == 0b1110 && check(i, 3, data)) i += 3;
                        else if (data[i] >> 3 == 0b11110 && check(i, 4, data)) i += 4;
                            else return false;
        }
        return true;
    }
};

results matching ""

    No results matching ""