Skip to content

Commit e7d446b

Browse files
committed
add 0232
1 parent 4b8a16e commit e7d446b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

algorithms/0232/README.md

Whitespace-only changes.

algorithms/0232/main.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package _0232
2+
3+
type MyQueue struct {
4+
in []int
5+
out []int
6+
}
7+
8+
/** Initialize your data structure here. */
9+
func Constructor() MyQueue {
10+
return MyQueue{
11+
in: make([]int, 0),
12+
out: make([]int, 0),
13+
}
14+
}
15+
16+
/** Push element x to the back of queue. */
17+
func (this *MyQueue) Push(x int) {
18+
this.in = append(this.in, x)
19+
}
20+
21+
/** Removes the element from in front of queue and returns that element. */
22+
func (this *MyQueue) Pop() int {
23+
if len(this.out) > 0 {
24+
x := this.out[len(this.out)-1]
25+
this.out = this.out[0 : len(this.out)-1]
26+
return x
27+
}
28+
this.in2out()
29+
return this.Pop()
30+
}
31+
func (this *MyQueue) in2out() {
32+
for len(this.in) > 0 {
33+
this.out = append(this.out, this.in[len(this.in)-1])
34+
this.in = this.in[:len(this.in)-1]
35+
}
36+
}
37+
38+
/** Get the front element. */
39+
func (this *MyQueue) Peek() int {
40+
if len(this.out) > 0 {
41+
return this.out[len(this.out)-1]
42+
}
43+
this.in2out()
44+
return this.Peek()
45+
}
46+
47+
/** Returns whether the queue is empty. */
48+
func (this *MyQueue) Empty() bool {
49+
return len(this.out) == 0 && len(this.in) == 0
50+
}
51+
52+
/**
53+
* Your MyQueue object will be instantiated and called as such:
54+
* obj := Constructor();
55+
* obj.Push(x);
56+
* param_2 := obj.Pop();
57+
* param_3 := obj.Peek();
58+
* param_4 := obj.Empty();
59+
*/

0 commit comments

Comments
 (0)