【leetcode】面试题 03.06. 动物收容所

 

typedef struct {
    int front[2];
    int rear[2];
    int* arr[2][20001];
    int noAnimal[2];
} AnimalShelf;

AnimalShelf* animalShelfCreate() {
    AnimalShelf* obj=(AnimalShelf*)calloc(sizeof(AnimalShelf),1);
    obj->noAnimal[0]=-1;
    obj->noAnimal[1]=-1;
    return obj;
}

void animalShelfEnqueue(AnimalShelf* obj, int* animal, int animalSize) {
    obj->arr[animal[1]][obj->rear[animal[1]]++]=animal;
}

int* animalShelfDequeueAny(AnimalShelf* obj, int* retSize) {
    *retSize=2;
    if(obj->front[0]!=obj->rear[0] && obj->front[1]!=obj->rear[1]){
        if(obj->arr[0][obj->front[0]][0] < obj->arr[1][obj->front[1]][0])
            return obj->arr[0][obj->front[0]++];
        else 
            return obj->arr[1][obj->front[1]++];
    }
    else if(obj->front[0]!=obj->rear[0]){
        return obj->arr[0][obj->front[0]++];
    }
    else if(obj->front[1]!=obj->rear[1]){
        return obj->arr[1][obj->front[1]++];
    }
    else
        return obj->noAnimal;
}

int* animalShelfDequeueDog(AnimalShelf* obj, int* retSize) {
    *retSize=2;
    return (obj->front[1]!=obj->rear[1])?obj->arr[1][obj->front[1]++] :obj->noAnimal;
}

int* animalShelfDequeueCat(AnimalShelf* obj, int* retSize) {
    *retSize=2;
    return (obj->front[0]!=obj->rear[0])?obj->arr[0][obj->front[0]++] :obj->noAnimal;
}

void animalShelfFree(AnimalShelf* obj) {
    free(obj);
}

 

posted @ 2020-12-02 10:46  温暖了寂寞  阅读(154)  评论(0编辑  收藏  举报