naive c++ solution


  • 0
    K
    class Solution {
    public:
        bool validUtf8(vector<int>& data) {
            int i = 0;
            while(i < data.size())
            {   
                data[i] = data[i] & 255;
                if(data[i] < 128) i++;
                else if((data[i] & 64) == 0) return false;
                else
                {
                    int n = 0;
                    int pos = 64;
                    while((data[i] & pos) != 0)
                    {
                        pos >>= 1;
                        n++;
                    }    
                    i++;
                    while(i < data.size() && n--)
                    {
                        if(data[i] < 128 || (data[i] & 64) != 0) return false;
                        i++;
                    }
                    if(n > 0) return false;
                }
                
            }
            return true;
            
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.