-
Notifications
You must be signed in to change notification settings - Fork 0
/
225.implement-stack-using-queues.go
62 lines (49 loc) · 1.1 KB
/
225.implement-stack-using-queues.go
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
/*
* @lc app=leetcode id=225 lang=golang
*
* [225] Implement Stack using Queues
*/
// @lc code=start
type MyStack struct {
enqueue []int
dequeue []int
}
func Constructor() MyStack {
return MyStack{[]int{}, []int{}}
}
func (this *MyStack) Push(x int) {
this.enqueue = append(this.enqueue, x)
}
/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
length := len(this.enqueue) - 1
for i := 0; i < length; i++ {
this.dequeue = append(this.dequeue, this.enqueue[0])
this.enqueue = this.enqueue[1:]
}
top := this.enqueue[0]
this.enqueue = this.dequeue
this.dequeue = nil
return top
}
/** Get the top element. */
func (this *MyStack) Top() int {
peek := this.Pop()
this.enqueue = append(this.enqueue, peek)
return peek
}
func (this *MyStack) Empty() bool {
if len(this.enqueue) == 0 {
return true
}
return false
}
/**
* Your MyStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.Empty();
*/
// @lc code=end