UTF-8 Validation Solutions in C++
Number 393
Difficulty Medium
Acceptance 37.5%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/utf-8-validation/// Author : Hao Chen// Date : 2016-09-08class Solution {public:bool validUtf8(vector<int>& data) {int i = 0;while ( i < data.size() ) {if ( (data[i] & 0x80) == 0 ){i++;continue;}int len = 0;if ( (data[i] & 0xE0) == 0xC0 ) { // checking 110xxxxxlen = 2;}else if ( (data[i] & 0xF0) == 0xE0) { // checking 1110xxxxlen = 3;}else if ( (data[i] & 0xF8) == 0xF0) { // checking 11110xxxlen = 4;}else {return false;}for (int j=i+1; j < i+len; j++) { //checking 10xxxxxxif ( (data[j] & 0xC0) != 0x80 ) {return false;}}i += len ;if (i > data.size()) {return false;}}return true;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/utf-8-validation//// Author : liuyubobobo/// Time : 2018-10-31#include <iostream>#include <vector>using namespace std;/// Using Binary String/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:bool validUtf8(vector<int>& data) {for(int i = 0; i < data.size(); ){string byte = get_binary_str(data[i]);if(byte[0] == '0')i ++;else if(byte.substr(0, 3) == "110"){if(i + 1 >= data.size())return false;if(!is10(data[i + 1]))return false;i += 2;}else if(byte.substr(0, 4) == "1110"){if(i + 2 >= data.size())return false;for(int j = 1; j <= 2; j ++)if(!is10(data[i + j]))return false;i += 3;}else if(byte.substr(0, 5) == "11110"){if(i + 3 >= data.size())return false;for(int j = 1; j <= 3; j ++)if(!is10(data[i + j]))return false;i += 4;}elsereturn false;}return true;}private:bool is10(int byte){string s = get_binary_str(byte);return s[0] == '1' && s[1] == '0';}string get_binary_str(int data){string ret = "";for(int i = 0; i < 8; i ++){ret += '0' + data % 2;data /= 2;}reverse(ret.begin(), ret.end());return ret;}};int main() {vector<int> data1 = {197, 130, 1};// 11000101 10000010 00000001cout << Solution().validUtf8(data1) << endl;// truereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/utf-8-validation//// Author : liuyubobobo/// Time : 2018-10-31#include <iostream>#include <vector>using namespace std;/// Using Bit Manipulation/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:bool validUtf8(vector<int>& data) {for(int i = 0; i < data.size(); ){int byte = data[i] & 0b11111111;if(!(byte & 0b10000000))i ++;else if((byte & 0b11000000) == 0b11000000 && !(byte & 0b00100000)){if(i + 1 >= data.size())return false;if(!is10(data[i + 1]))return false;i += 2;}else if((byte & 0b11100000) == 0b11100000 && !(byte & 0b00010000)){if(i + 2 >= data.size())return false;for(int j = 1; j <= 2; j ++)if(!is10(data[i + j]))return false;i += 3;}else if((byte & 0b11110000) == 0b11110000 && !(byte & 0b00001000)){if(i + 3 >= data.size())return false;for(int j = 1; j <= 3; j ++)if(!is10(data[i + j]))return false;i += 4;}elsereturn false;}return true;}private:bool is10(int byte){return (byte & 0b10000000) && !(byte & 0b01000000);}};int main() {vector<int> data1 = {197, 130, 1};// 11000101 10000010 00000001cout << Solution().validUtf8(data1) << endl;// truereturn 0;}