列表

详情


面试题 03.06. 动物收容所

动物收容所。有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,实现各种操作方法,比如enqueuedequeueAnydequeueDogdequeueCat。允许使用Java内置的LinkedList数据结构。

enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。

dequeue*方法返回一个列表[动物编号, 动物种类],若没有可以收养的动物,则返回[-1,-1]

示例1:

 输入:
["AnimalShelf", "enqueue", "enqueue", "dequeueCat", "dequeueDog", "dequeueAny"]
[[], [[0, 0]], [[1, 0]], [], [], []]
 输出:
[null,null,null,[0,0],[-1,-1],[1,0]]

示例2:

 输入:
["AnimalShelf", "enqueue", "enqueue", "enqueue", "dequeueDog", "dequeueCat", "dequeueAny"]
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
 输出:
[null,null,null,null,[2,1],[0,0],[1,0]]

说明:

  1. 收纳所的最大容量为20000

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class AnimalShelf { public: AnimalShelf() { } void enqueue(vector<int> animal) { } vector<int> dequeueAny() { } vector<int> dequeueDog() { } vector<int> dequeueCat() { } }; /** * Your AnimalShelf object will be instantiated and called as such: * AnimalShelf* obj = new AnimalShelf(); * obj->enqueue(animal); * vector<int> param_2 = obj->dequeueAny(); * vector<int> param_3 = obj->dequeueDog(); * vector<int> param_4 = obj->dequeueCat(); */

golang 解法, 执行用时: 88 ms, 内存消耗: 10.5 MB, 提交时间: 2021-06-16 15:35:39

type AnimalShelf struct {
	listAnimal *list.List
}


func Constructor() AnimalShelf {
	return AnimalShelf{
		listAnimal:list.New(),
	}
}


func (this *AnimalShelf) Enqueue(animal []int)  {
	this.listAnimal.PushBack(animal)
}


func (this *AnimalShelf) DequeueAny() []int {
	if this.listAnimal.Len() == 0 {
		return []int{-1, -1}
	}

	animal := this.listAnimal.Front()
	defer this.listAnimal.Remove(animal)
	return animal.Value.([]int)
}


func (this *AnimalShelf) DequeueDog() []int {
	for animal := this.listAnimal.Front(); animal != nil; animal = animal.Next(){
		if animal.Value.([]int)[1] == 1 {
			defer this.listAnimal.Remove(animal)
			return animal.Value.([]int)
		}
	}
	return []int{-1, -1}
}


func (this *AnimalShelf) DequeueCat() []int {
	for animal := this.listAnimal.Front(); animal != nil; animal = animal.Next(){
		if animal.Value.([]int)[1] == 0 {
			defer this.listAnimal.Remove(animal)
			return animal.Value.([]int)
		}
	}
	return []int{-1, -1}
}


/**
 * Your AnimalShelf object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Enqueue(animal);
 * param_2 := obj.DequeueAny();
 * param_3 := obj.DequeueDog();
 * param_4 := obj.DequeueCat();
 */

golang 解法, 执行用时: 84 ms, 内存消耗: 10.7 MB, 提交时间: 2021-06-16 15:33:35

type AnimalShelf struct {
    cats [][]int
    dogs [][]int
    all [][]int
}


func Constructor() AnimalShelf {
    return AnimalShelf{
        cats: [][]int{},
        dogs: [][]int{},
        all: [][]int{},
    }
}


func (this *AnimalShelf) Enqueue(animal []int)  {
    if animal[1] == 0 {
        this.cats = append(this.cats, animal)
    } else {
        this.dogs = append(this.dogs, animal)
    }
    this.all = append(this.all, animal)
}


func (this *AnimalShelf) DequeueAny() []int {
    animal := []int{-1, -1}
    n := len(this.all)
    if n > 0 {
        animal = this.all[0]

        if animal[1] == 0 {
            this.cats = this.cats[1:]
        } else {
            this.dogs = this.dogs[1:]
        }
        this.all = this.all[1:]
    }


    return animal
}


func (this *AnimalShelf) DequeueDog() []int {
    animal := []int{-1, -1}
    n := len(this.dogs)
    if n > 0 {
        animal = this.dogs[0]
        this.dogs = this.dogs[1:]
        for i := 0; i < len(this.all); i++ {
            if animal[0] == this.all[i][0] && animal[1] == this.all[i][1] {
                this.all = append(this.all[:i], this.all[i+1:]...)
                break
            }
        }
    }

    return animal
}


func (this *AnimalShelf) DequeueCat() []int {
    animal := []int{-1, -1}
    n := len(this.cats)
    if n > 0 {
        animal = this.cats[0]
        this.cats = this.cats[1:]
        for i := 0; i < len(this.all); i++ {
            if animal[0] == this.all[i][0] && animal[1] == this.all[i][1] {
                this.all = append(this.all[:i], this.all[i+1:]...)
                break
            }
        }
    }

    return animal
}


/**
 * Your AnimalShelf object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Enqueue(animal);
 * param_2 := obj.DequeueAny();
 * param_3 := obj.DequeueDog();
 * param_4 := obj.DequeueCat();
 */

上一题