diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-16 06:33:42 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-16 06:33:42 +0000 |
commit | 00eaaa6668d1c024b7bfd67d3cbded569b06ccbb (patch) | |
tree | b1d2fda7fae75c73e34e47a41809ab3e3b60994f /232 | |
parent | 1143 (diff) | |
download | leetcode-00eaaa6668d1c024b7bfd67d3cbded569b06ccbb.tar.gz leetcode-00eaaa6668d1c024b7bfd67d3cbded569b06ccbb.zip |
232
Diffstat (limited to '232')
-rw-r--r-- | 232/go.mod | 5 | ||||
-rw-r--r-- | 232/go.sum | 2 | ||||
-rw-r--r-- | 232/main.go | 93 |
3 files changed, 100 insertions, 0 deletions
diff --git a/232/go.mod b/232/go.mod new file mode 100644 index 0000000..8de9603 --- /dev/null +++ b/232/go.mod @@ -0,0 +1,5 @@ +module 232 + +go 1.19 + +require github.com/emirpasic/gods v1.18.1 diff --git a/232/go.sum b/232/go.sum new file mode 100644 index 0000000..b5ad666 --- /dev/null +++ b/232/go.sum @@ -0,0 +1,2 @@ +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= diff --git a/232/main.go b/232/main.go new file mode 100644 index 0000000..718e526 --- /dev/null +++ b/232/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + + "github.com/emirpasic/gods/stacks/arraystack" +) + +type MyQueue struct { + s1 *arraystack.Stack + s2 *arraystack.Stack +} + +func Constructor() MyQueue { + s1 := arraystack.New() + s2 := arraystack.New() + + q := MyQueue{s1: s1, s2: s2} + + return q +} + +func (this *MyQueue) Push(x int) { + this.s1.Push(x) +} + +func (this *MyQueue) Pop() int { + for { + if val, ok := this.s1.Pop(); ok { + this.s2.Push(val) + } else { + break + } + } + + val, _ := this.s2.Pop() + + for { + if val, ok := this.s2.Pop(); ok { + this.s1.Push(val) + } else { + break + } + } + + return val.(int) +} + +func (this *MyQueue) Peek() int { + for { + if val, ok := this.s1.Pop(); ok { + this.s2.Push(val) + } else { + break + } + } + + val, _ := this.s2.Pop() + this.s2.Push(val) + + for { + if val, ok := this.s2.Pop(); ok { + this.s1.Push(val) + } else { + break + } + } + + return val.(int) + +} + +func (this *MyQueue) Empty() bool { + return this.s1.Empty() +} + +/** + * 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() { + obj := Constructor() + obj.Push(1) + obj.Push(2) + fmt.Println(obj.Peek()) + fmt.Println(obj.Pop()) + fmt.Println(obj.Empty()) +} |