반응형
https://leetcode.com/problems/utf-8-validation/
UTF-8 Validation - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
public:
bool validUtf8(vector<int>& data) {
int cnt = 0;
for(int i=0; i<data.size(); i++) {
if(cnt == 0) {
if(((data[i] >> 7) & 1)) {
for(int j=6; j>=0; j--) {
int val = (data[i] >> j) & 1;
if(val == 0) break;
cnt++;
}
if(cnt == 0 || cnt > 3) return false;
}
} else {
cnt--;
if(((data[i] >> 6) & 3) != 2) // mask bits 11, last 2 bits 10
return false;
}
}
return cnt == 0;
}
};반응형
'Algorithm' 카테고리의 다른 글
| LeetCode 653 : Two Sum IV - Input is a BST (0) | 2022.10.09 |
|---|---|
| LeetCode 16 : 3Sum Closest (1) | 2022.10.08 |
| LeetCode 1996 : The Number of Weak Characters in the Game (0) | 2022.09.09 |
| LeetCode 814 : Binary Tree Pruning (0) | 2022.09.06 |
| LeetCode 987 : Vertical Order Traversal of a Binary Tree (0) | 2022.09.04 |