C code easy understand


  • 0
    A
    bool isOneBitCharacter(int* bits, int bitsSize) {
        
        if(bits[bitsSize-1]==1)
            return false;
        
        for(int i = 0; i< bitsSize; i++)
        {
            if(bits[i]==1)
            {
                if(i+2==bitsSize){
                    return false;
                }
                i++;
            }
        }
        return true;
    }
    

  • 0

    I started modifying it a little, then a little more, and eventually ended up with something quite different :-)

    bool isOneBitCharacter(int* bits, int bitsSize) {
        while (bitsSize--)
            if (*bits++ && bits++)
                if (!--bitsSize)
                    return false;
        return true;
    }
    

    I somehow like code that doesn't have numbers.

    Another one:

    bool isOneBitCharacter(int* bits, int bitsSize) {
        while (bitsSize-- > 2)
            bitsSize -= *bits++ && bits++;
        return !*bits;
    }
    

Log in to reply
 

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