leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0393.cpp (666B)
0 class Solution { 1 public: 2 bool validUtf8(const vector<int> &data) const { 3 static const uint8_t mask[] = {0x80, 0xE0, 0xF0, 0xF8}; 4 static const uint8_t val[] = {0x00, 0xC0, 0xE0, 0xF0}; 5 const int n = size(data); 6 7 for (int i = 0, j, k; i < n; i++) { 8 for (j = 0; j < 4; j++) { 9 if ((data[i] & mask[j]) != val[j]) continue; 10 break; 11 } 12 if (j == 4) return false; 13 14 for (k = 0; k < j && i + 1 < n; k++) { 15 if ((data[++i] & 0xC0) != 0x80) return false; 16 } 17 if (k != j) return false; 18 } 19 20 return true; 21 } 22 };