/
githubmirror
/
LeetCode-Go
Обзор
Документация
Войти
/
githubmirror
/
LeetCode-Go
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/0232.Implement-Queue-using-Stacks/232. Implement Queue using Stacks.go
51 строка
1 KB
YDZ
规范格式
07 авг 2020, 12:06
07 авг 2020, 12:06
4e11f40
Код
Авторство
О чём код?
package leetcode type MyQueue struct { Stack *[]int Queue *[]int } /** Initialize your data structure here. */ func Constructor232() MyQueue { tmp1, tmp2 := []int{}, []int{} return MyQueue{Stack: &tmp1, Queue: &tmp2} } /** Push element x to the back of queue. */ func (this *MyQueue) Push(x int) { *this.Stack = append(*this.Stack, x) } /** Removes the element from in front of queue and returns that element. */ func (this *MyQueue) Pop() int { if len(*this.Queue) == 0 { this.fromStackToQueue(this.Stack, this.Queue) } popped := (*this.Queue)[len(*this.Queue)-1] *this.Queue = (*this.Queue)[:len(*this.Queue)-1] return popped } /** Get the front element. */ func (this *MyQueue) Peek() int { if len(*this.Queue) == 0 { this.fromStackToQueue(this.Stack, this.Queue) } return (*this.Queue)[len(*this.Queue)-1] } /** Returns whether the queue is empty. */ func (this *MyQueue) Empty() bool { return len(*this.Stack)+len(*this.Queue) == 0 } func (this *MyQueue) fromStackToQueue(s, q *[]int) { for len(*s) > 0 { popped := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] *q = append(*q, popped) } }