From c1d7e0d360746396b4af0243fdfe0be2f9f39fdb Mon Sep 17 00:00:00 2001 From: tomato <18185657+taoyq1988@users.noreply.github.com> Date: Thu, 12 Jun 2025 12:00:59 +0800 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.0281 No.0281.Zigzag Iterator --- .../0200-0299/0281.Zigzag Iterator/README.md | 40 +++++++++++++++++++ .../0281.Zigzag Iterator/README_EN.md | 40 +++++++++++++++++++ .../0281.Zigzag Iterator/Solution.go | 35 ++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 solution/0200-0299/0281.Zigzag Iterator/Solution.go diff --git a/solution/0200-0299/0281.Zigzag Iterator/README.md b/solution/0200-0299/0281.Zigzag Iterator/README.md index 58c0422f2a7ad..0d6b337ec4a93 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README.md @@ -207,6 +207,46 @@ impl ZigzagIterator { } ``` +#### Go + +```go +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1 []int, 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 +} +``` + diff --git a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md index 577e8df1e3bd6..b3f7ba53eb93d 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md @@ -221,6 +221,46 @@ impl ZigzagIterator { } ``` +#### Go + +```go +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1 []int, 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 +} +``` + 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..1c4bf7febac24 --- /dev/null +++ b/solution/0200-0299/0281.Zigzag Iterator/Solution.go @@ -0,0 +1,35 @@ +type ZigzagIterator struct { + cur int + size int + indexes []int + vectors [][]int +} + +func Constructor(v1 []int, 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 +} From 429b7339997f5799b60a584e803b657ffa94cede Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Jun 2025 12:58:35 +0800 Subject: [PATCH 2/4] Update README.md --- .../0200-0299/0281.Zigzag Iterator/README.md | 88 ++++++++++--------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/solution/0200-0299/0281.Zigzag Iterator/README.md b/solution/0200-0299/0281.Zigzag Iterator/README.md index 0d6b337ec4a93..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 @@ -207,46 +255,6 @@ impl ZigzagIterator { } ``` -#### Go - -```go -type ZigzagIterator struct { - cur int - size int - indexes []int - vectors [][]int -} - -func Constructor(v1 []int, 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 -} -``` - From 8ebebb0688036f936f9fae69387d105ad03f3610 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Jun 2025 12:59:02 +0800 Subject: [PATCH 3/4] Update README_EN.md --- .../0281.Zigzag Iterator/README_EN.md | 88 ++++++++++--------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md index b3f7ba53eb93d..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 @@ -221,46 +269,6 @@ impl ZigzagIterator { } ``` -#### Go - -```go -type ZigzagIterator struct { - cur int - size int - indexes []int - vectors [][]int -} - -func Constructor(v1 []int, 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 -} -``` - From 59d2d906dc4d968b0c46b5845e24411d9334f4e7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Jun 2025 12:59:16 +0800 Subject: [PATCH 4/4] Update Solution.go --- .../0200-0299/0281.Zigzag Iterator/Solution.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/solution/0200-0299/0281.Zigzag Iterator/Solution.go b/solution/0200-0299/0281.Zigzag Iterator/Solution.go index 1c4bf7febac24..814dd960d3a17 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/Solution.go +++ b/solution/0200-0299/0281.Zigzag Iterator/Solution.go @@ -5,7 +5,7 @@ type ZigzagIterator struct { vectors [][]int } -func Constructor(v1 []int, v2 []int) *ZigzagIterator { +func Constructor(v1, v2 []int) *ZigzagIterator { return &ZigzagIterator{ cur: 0, size: 2, @@ -14,7 +14,7 @@ func Constructor(v1 []int, v2 []int) *ZigzagIterator { } } -func (this *ZigzagIterator) Next() int { +func (this *ZigzagIterator) next() int { vector := this.vectors[this.cur] index := this.indexes[this.cur] res := vector[index] @@ -23,7 +23,7 @@ func (this *ZigzagIterator) Next() int { return res } -func (this *ZigzagIterator) HasNext() bool { +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 @@ -33,3 +33,11 @@ func (this *ZigzagIterator) HasNext() bool { } 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()) + * } + */