Leetcode Note: Go - Implement Queue Using Stacks
Implement Queue using Stacks - LeetCode
https://leetcode.com/problems/implement-queue-using-stacks/
- Go 言語で取り組んだメモ
所感
- Stack を使って Queue を実装する
回答
Implement Queue using Stacks - LeetCode
https://leetcode.com/problems/implement-queue-using-stacks/solution/
golang beats 100% time and memory - LeetCode Discuss
https://leetcode.com/problems/implement-queue-using-stacks/discuss/316889/golang-beats-100-time-and-memory
type MyQueue struct {
stackPush, stackPop []int
}
func Constructor() MyQueue {
return MyQueue{}
}
func (this *MyQueue) Push(x int) {
this.stackPush = append(this.stackPush, x)
}
func (this *MyQueue) Pop() int {
ans := this.Peek()
this.stackPop = this.stackPop[:len(this.stackPop) - 1]
return ans
}
func (this *MyQueue) Peek() int {
if len(this.stackPop) == 0 {
for len(this.stackPush) != 0 {
this.stackPop = append(this.stackPop, this.stackPush[len(this.stackPush) - 1])
this.stackPush = this.stackPush[:len(this.stackPush) - 1]
}
}
return this.stackPop[len(this.stackPop) - 1]
}
func (this *MyQueue) Empty() bool {
return len(this.stackPush) == 0 && len(this.stackPop) == 0
}
/**
* 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();
*/