1-bit and 2-bit Characters


  • 1

    Click here to see the full article post


  • 0

    A few variations of approach #2:

    Using index:

        def isOneBitCharacter(self, bits):
            return not (bits[-2::-1] + [0]).index(0) % 2
    

    Using iter:

        def isOneBitCharacter(self, bits):
            r = reversed(bits).next
            return not sum(iter(r, r())) % 2
    

    Using takewhile:

    from itertools import takewhile
    
    class Solution:
        def isOneBitCharacter(self, bits):
            it = reversed(bits)
            return not sum(takewhile(int, it), next(it)) % 2
    

    Using reduce:

    from operator import xor
    from itertools import takewhile
    
    class Solution:
        def isOneBitCharacter(self, bits):
            it = reversed(bits)
            return not reduce(xor, takewhile(int, it), next(it))
    

  • -1
    V

    while (i<len) {
    if (a[i]) {
    i+=2;
    isOneChar = 0;
    } else {
    i++;
    isOneChar=1;
    }
    }


  • 0
    D

    from functools import reduce
    return reduce(lambda x, y: 0 - y if x >= 0 else 1, bits, 0) == 0


  • 0
    X

    seems my solution is too overcomplicated... QAQ

    bool isOneBitCharacter(vector<int>& bits) {
            vector<int> new_vec = bits;
            while (!new_vec.empty() || new_vec.size() == 1) {
                if (new_vec[0] == 1 && new_vec.size() > 1) {
                    new_vec.erase(new_vec.begin());
                    new_vec.erase(new_vec.begin());
                    continue;
                }
                else if (new_vec[0] == 0 && new_vec.size() > 1) {
                    new_vec.erase(new_vec.begin());
                    continue;
                } else {
                    break;
                }
            }
            if (new_vec.empty()) {
                return false;
            } else return new_vec[0] == 0;
        }
    

  • -1
    C

    bool isOneBitCharacter(int* bits, int bitsSize) {
    int i=0;
    bool result;
    if(bitsSize==1)
    return true;
    while(i<bitsSize)
    {

        if(i==bitsSize-1)
            result= true;
        if((i==bitsSize-2)&&bits[i]==1)
            result= false;
        if((i==bitsSize-2)&&bits[i]==0)
            result= true;
        if(bits[i]==1)
        i+=2;
        if(bits[i]==0)
            i+=1;
    }
    return result;
    

    }


  • -1
    Q

    There is no need to iterate through the whole array. Whenever there is a 0, it means this 0 belongs to a 2-bit Character along with its front element 1,or itself represents a 1-bit Character. So we could just count the number of 1 before the last 0, if the count is an even number, than the last 0 itself represents a 1-bit Character and return true,otherwise return false.
    if(bits.length==0 || bits==null){
    return false;
    }
    int pos = bits.length-1;
    int count = 0;
    while(pos>0){
    pos--;
    if(bits[pos]==1){
    count++;
    }else{
    break;
    }
    }

    	 return count%2==0;

  • -1
    G

    class Solution {
    public boolean isOneBitCharacter(int[] bits) {
    for(int i=0;i< bits.length;i++){
    if(bits[i] == 0) continue;
    if(i >= bits.length - 2) return false;
    i++;
    }
    return true;
    }
    }


Log in to reply
 

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