mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
规范格式
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user