-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI0304-ImplementQueueUsingStacks.go
More file actions
101 lines (89 loc) · 2.84 KB
/
LCCI0304-ImplementQueueUsingStacks.go
File metadata and controls
101 lines (89 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
// 面试题 03.04. Implement Queue using Stacks LCCI
// Implement a MyQueue class which implements a queue using two stacks.
// Example:
// MyQueue queue = new MyQueue();
// queue.push(1);
// queue.push(2);
// queue.peek(); // return 1
// queue.pop(); // return 1
// queue.empty(); // return false
// Notes:
// 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).
import "fmt"
type MyQueue struct {
stack1 []int
stack2 []int
}
/** Initialize your data structure here. */
func Constructor() MyQueue {
return MyQueue{}
}
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
// 入栈1
this.stack1 = append(this.stack1, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
// 如果stack2不为空,直接返回栈顶元素
if len(this.stack2) != 0 {
x := this.stack2[len(this.stack2) - 1]
this.stack2 = this.stack2[:len(this.stack2) - 1]
return x
}
for len(this.stack1) != 0 {
// 将所有栈1的元素都入2栈
this.stack2 = append(this.stack2, this.stack1[len(this.stack1) - 1])
this.stack1 = this.stack1[:len(this.stack1) - 1]
}
return this.Pop()
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
if len(this.stack2) != 0 {
return this.stack2[len(this.stack2) - 1]
}
for len(this.stack1) != 0 {
// 将所有栈1的元素都入2栈
this.stack2 = append(this.stack2, this.stack1[len(this.stack1) - 1])
this.stack1 = this.stack1[:len(this.stack1) - 1]
}
return this.Peek()
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
if len(this.stack1) == 0 && len(this.stack2) == 0 {
return true
}
return false
}
/**
* 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();
*/
func main() {
// Example:
// MyQueue queue = new MyQueue();
obj := Constructor()
fmt.Println(obj)
// queue.push(1);
obj.Push(1)
fmt.Println(obj)
// queue.push(2);
obj.Push(2)
fmt.Println(obj)
// queue.peek(); // return 1
fmt.Println(obj.Peek()) // 1
// queue.pop(); // return 1
fmt.Println(obj.Pop()) // 1
fmt.Println(obj)
// queue.empty(); // return false
fmt.Println(obj.Empty()) // false
}