diff --git a/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks.go b/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks.go new file mode 100644 index 00000000..22eaed4e --- /dev/null +++ b/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks.go @@ -0,0 +1,51 @@ +package leetcode + +type MyQueue struct { + Stack *[]int + Queue *[]int +} + +/** Initialize your data structure here. */ +func Constructor_232() 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) + } +} diff --git a/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks_test.go b/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks_test.go new file mode 100644 index 00000000..e9404ff3 --- /dev/null +++ b/Algorithms/232. Implement Queue using Stacks/232. Implement Queue using Stacks_test.go @@ -0,0 +1,21 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +func Test_Problem232(t *testing.T) { + obj := Constructor_232() + fmt.Printf("obj = %v\n", obj) + obj.Push(2) + fmt.Printf("obj = %v\n", obj) + obj.Push(10) + fmt.Printf("obj = %v\n", obj) + param_2 := obj.Pop() + fmt.Printf("param_2 = %v\n", param_2) + param_3 := obj.Peek() + fmt.Printf("param_3 = %v\n", param_3) + param_4 := obj.Empty() + fmt.Printf("param_4 = %v\n", param_4) +} diff --git a/Algorithms/232. Implement Queue using Stacks/README.md b/Algorithms/232. Implement Queue using Stacks/README.md new file mode 100644 index 00000000..015f7f59 --- /dev/null +++ b/Algorithms/232. Implement Queue using Stacks/README.md @@ -0,0 +1,36 @@ +# [232. Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) + +## 题目 + +Implement the following operations of a queue using stacks. + +- push(x) -- Push element x to the back of queue. +- pop() -- Removes the element from in front of queue. +- peek() -- Get the front element. +- empty() -- Return whether the queue is empty. + +Example : + +```c +MyQueue queue = new MyQueue(); + +queue.push(1); +queue.push(2); +queue.peek(); // returns 1 +queue.pop(); // returns 1 +queue.empty(); // returns false +``` + +Note: + +- You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid. +- Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. +- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). + + +## 题目大意 + +题目要求用栈实现一个队列的基本操作:push(x)、pop()、peek()、empty()。 + + +