zoukankan      html  css  js  c++  java
  • 【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);
    }
  • 相关阅读:
    左式堆
    winsock库
    二叉堆
    关键字explicit
    HDOJ 1012
    HDOJ 1013
    STL priority实例
    二项队列
    ASP.NET Session过期问题揭秘
    RenderControl (asp.net)
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14072433.html
Copyright © 2011-2022 走看看