Quick c++ solution


  • 0
    H
    • If the last digit is 1, then the last character should be 11.
    • Scan the array.When we meet the digit 1, skip the next digit. Otherwise check if this is the last digit of the array.If it is, return true.
    class Solution {
    public:
        bool isOneBitCharacter(vector<int>& bits) {
            
            int count = bits.size();
            if(bits[count-1] == 1)
            {
                return false; // do a quick check first.
            }
            
            for (int i = 0; i < count; i++)
            {
                if(bits[i] == 1)
                {
                    i++;
                }
                else
                {
                    if (i == count-1)
                    {
                        return true;
                    }
                }
            }
            
            return false;
        }
    };
    

Log in to reply
 

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