12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- type MyQueue struct {
- Stack MyStack
- Queue MyStack
- }
- /** Initialize your data structure here. */
- func Constructor() MyQueue {
- return MyQueue{make([]int, 0), make([]int, 0)}
- }
- /** Push element x to the back of queue. */
- func (this *MyQueue) Push(x int) {
- for !this.Queue.Empty() {
- this.Stack.Push(this.Queue.Pop())
- }
- this.Stack.Push(x)
- }
- /** Removes the element from in front of queue and returns that element. */
- func (this *MyQueue) Pop() int {
- for !this.Stack.Empty() {
- this.Queue.Push(this.Stack.Pop())
- }
- return this.Queue.Pop()
- }
- /** Get the front element. */
- func (this *MyQueue) Peek() int {
- for !this.Stack.Empty() {
- this.Queue.Push(this.Stack.Pop())
- }
- return this.Queue.Peek()
- }
- /** Returns whether the queue is empty. */
- func (this *MyQueue) Empty() bool {
- return this.Stack.Empty() && this.Queue.Empty()
- }
- /**
- * 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();
- */
- type MyStack []int
- func (s MyStack) Size() int {
- return len(s)
- }
- func (s MyStack) Empty() bool {
- return len(s) == 0
- }
- func (s MyStack) Peek() int {
- return s[len(s)-1]
- }
- func (s *MyStack) Push(x int) {
- *s = append(*s, x)
- }
- func (s *MyStack) Pop() int {
- n := len(*s)
- top := (*s)[n-1]
- *s = (*s)[:n-1]
- return top
- }
|