上次编辑到这里,代码来自缓存 点击恢复默认模板
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();
*/