Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool validUtf8(vector<int>& data) {
- if (data.size() == 0)
- return 0;
- int cur = 0;
- while (cur != data.size()) {
- int x = data[cur];
- if (!((x >> 7) & 1)) {
- cur++;
- continue;
- }
- int n = 5;
- for (int i = 6; i >= 0; i--)
- if (!((x >> i) & 1)) {
- n = 7 - i;
- break;
- }
- if (n > 4 || n == 1)
- return 0;
- cur++;
- for (int i = 1; i < n; i++) {
- if (cur >= data.size())
- return 0;
- int x = data[cur];
- if ((x >> 6) != 2)
- return 0;
- cur++;
- }
- }
- return 1;
- }
- };
Add Comment
Please, Sign In to add comment