diff --git a/solution/0200-0299/0281.Zigzag Iterator/README.md b/solution/0200-0299/0281.Zigzag Iterator/README.md index 58c0422f2a7ad..63babb7957b71 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README.md @@ -149,6 +149,54 @@ public class ZigzagIterator { */ ``` +#### Go + +```go +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1, v2 []int) *ZigzagIterator { + return &ZigzagIterator{ + cur: 0, + size: 2, + indexes: []int{0, 0}, + vectors: [][]int{v1, v2}, + } +} + +func (this *ZigzagIterator) next() int { + vector := this.vectors[this.cur] + index := this.indexes[this.cur] + res := vector[index] + this.indexes[this.cur]++ + this.cur = (this.cur + 1) % this.size + return res +} + +func (this *ZigzagIterator) hasNext() bool { + start := this.cur + for this.indexes[this.cur] == len(this.vectors[this.cur]) { + this.cur = (this.cur + 1) % this.size + if start == this.cur { + return false + } + } + return true +} + +/** + * Your ZigzagIterator object will be instantiated and called as such: + * obj := Constructor(param_1, param_2); + * for obj.hasNext() { + * ans = append(ans, obj.next()) + * } + */ +``` + #### Rust ```rust diff --git a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md index 577e8df1e3bd6..46a607542a76e 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md @@ -163,6 +163,54 @@ public class ZigzagIterator { */ ``` +#### Go + +```go +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1, v2 []int) *ZigzagIterator { + return &ZigzagIterator{ + cur: 0, + size: 2, + indexes: []int{0, 0}, + vectors: [][]int{v1, v2}, + } +} + +func (this *ZigzagIterator) next() int { + vector := this.vectors[this.cur] + index := this.indexes[this.cur] + res := vector[index] + this.indexes[this.cur]++ + this.cur = (this.cur + 1) % this.size + return res +} + +func (this *ZigzagIterator) hasNext() bool { + start := this.cur + for this.indexes[this.cur] == len(this.vectors[this.cur]) { + this.cur = (this.cur + 1) % this.size + if start == this.cur { + return false + } + } + return true +} + +/** + * Your ZigzagIterator object will be instantiated and called as such: + * obj := Constructor(param_1, param_2); + * for obj.hasNext() { + * ans = append(ans, obj.next()) + * } + */ +``` + #### Rust ```rust diff --git a/solution/0200-0299/0281.Zigzag Iterator/Solution.go b/solution/0200-0299/0281.Zigzag Iterator/Solution.go new file mode 100644 index 0000000000000..814dd960d3a17 --- /dev/null +++ b/solution/0200-0299/0281.Zigzag Iterator/Solution.go @@ -0,0 +1,43 @@ +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1, v2 []int) *ZigzagIterator { + return &ZigzagIterator{ + cur: 0, + size: 2, + indexes: []int{0, 0}, + vectors: [][]int{v1, v2}, + } +} + +func (this *ZigzagIterator) next() int { + vector := this.vectors[this.cur] + index := this.indexes[this.cur] + res := vector[index] + this.indexes[this.cur]++ + this.cur = (this.cur + 1) % this.size + return res +} + +func (this *ZigzagIterator) hasNext() bool { + start := this.cur + for this.indexes[this.cur] == len(this.vectors[this.cur]) { + this.cur = (this.cur + 1) % this.size + if start == this.cur { + return false + } + } + return true +} + +/** + * Your ZigzagIterator object will be instantiated and called as such: + * obj := Constructor(param_1, param_2); + * for obj.hasNext() { + * ans = append(ans, obj.next()) + * } + */