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);
    }
  • 相关阅读:
    Java中ArrayList和LinkedList区别
    poi操作excel之: 将NUMERIC转换成TEXT
    Spring事务异常回滚,捕获异常不抛出就不会回滚
    CollectionUtils.select用法
    名词解释
    jenkins
    代码测试文章推荐
    redis 参考文章
    zookeeper,dubbo,dubbo admin
    Navicat For Mysql快捷键
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14072433.html
Copyright © 2011-2022 走看看