MaxObznyi

utf8-validation

Apr 14th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool validUtf8(vector<int>& data) {
  4. if (data.size() == 0)
  5. return 0;
  6. int cur = 0;
  7. while (cur != data.size()) {
  8. int x = data[cur];
  9. if (!((x >> 7) & 1)) {
  10. cur++;
  11. continue;
  12. }
  13. int n = 5;
  14. for (int i = 6; i >= 0; i--)
  15. if (!((x >> i) & 1)) {
  16. n = 7 - i;
  17. break;
  18. }
  19. if (n > 4 || n == 1)
  20. return 0;
  21. cur++;
  22. for (int i = 1; i < n; i++) {
  23. if (cur >= data.size())
  24. return 0;
  25. int x = data[cur];
  26. if ((x >> 6) != 2)
  27. return 0;
  28. cur++;
  29. }
  30. }
  31. return 1;
  32. }
  33. };
Add Comment
Please, Sign In to add comment