mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
@ -285,6 +285,73 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
type MyQueue struct {
|
||||||
|
stack []int
|
||||||
|
back []int
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Initialize your data structure here. */
|
||||||
|
func Constructor() MyQueue {
|
||||||
|
return MyQueue{
|
||||||
|
stack: make([]int, 0),
|
||||||
|
back: make([]int, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Push element x to the back of queue. */
|
||||||
|
func (this *MyQueue) Push(x int) {
|
||||||
|
for len(this.back) != 0 {
|
||||||
|
val := this.back[len(this.back)-1]
|
||||||
|
this.back = this.back[:len(this.back)-1]
|
||||||
|
this.stack = append(this.stack, val)
|
||||||
|
}
|
||||||
|
this.stack = append(this.stack, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Removes the element from in front of queue and returns that element. */
|
||||||
|
func (this *MyQueue) Pop() int {
|
||||||
|
for len(this.stack) != 0 {
|
||||||
|
val := this.stack[len(this.stack)-1]
|
||||||
|
this.stack = this.stack[:len(this.stack)-1]
|
||||||
|
this.back = append(this.back, val)
|
||||||
|
}
|
||||||
|
if len(this.back) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
val := this.back[len(this.back)-1]
|
||||||
|
this.back = this.back[:len(this.back)-1]
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the front element. */
|
||||||
|
func (this *MyQueue) Peek() int {
|
||||||
|
for len(this.stack) != 0 {
|
||||||
|
val := this.stack[len(this.stack)-1]
|
||||||
|
this.stack = this.stack[:len(this.stack)-1]
|
||||||
|
this.back = append(this.back, val)
|
||||||
|
}
|
||||||
|
if len(this.back) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
val := this.back[len(this.back)-1]
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether the queue is empty. */
|
||||||
|
func (this *MyQueue) Empty() bool {
|
||||||
|
return len(this.stack) == 0 && len(this.back) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MyQueue object will be instantiated and called as such:
|
||||||
|
* obj := Constructor();
|
||||||
|
* obj.Push(x);
|
||||||
|
* param_2 := obj.Pop();
|
||||||
|
* param_3 := obj.Peek();
|
||||||
|
* param_4 := obj.Empty();
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user