Skip to content

Commit c1d7e0d

Browse files
committed
feat: add solutions to lc problem: No.0281
No.0281.Zigzag Iterator
1 parent 102ce22 commit c1d7e0d

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

solution/0200-0299/0281.Zigzag Iterator/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,46 @@ impl ZigzagIterator {
207207
}
208208
```
209209

210+
#### Go
211+
212+
```go
213+
type ZigzagIterator struct {
214+
cur int
215+
size int
216+
indexes []int
217+
vectors [][]int
218+
}
219+
220+
func Constructor(v1 []int, v2 []int) *ZigzagIterator {
221+
return &ZigzagIterator{
222+
cur: 0,
223+
size: 2,
224+
indexes: []int{0, 0},
225+
vectors: [][]int{v1, v2},
226+
}
227+
}
228+
229+
func (this *ZigzagIterator) Next() int {
230+
vector := this.vectors[this.cur]
231+
index := this.indexes[this.cur]
232+
res := vector[index]
233+
this.indexes[this.cur]++
234+
this.cur = (this.cur + 1) % this.size
235+
return res
236+
}
237+
238+
func (this *ZigzagIterator) HasNext() bool {
239+
start := this.cur
240+
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
241+
this.cur = (this.cur + 1) % this.size
242+
if start == this.cur {
243+
return false
244+
}
245+
}
246+
return true
247+
}
248+
```
249+
210250
<!-- tabs:end -->
211251

212252
<!-- solution:end -->

solution/0200-0299/0281.Zigzag Iterator/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,46 @@ impl ZigzagIterator {
221221
}
222222
```
223223

224+
#### Go
225+
226+
```go
227+
type ZigzagIterator struct {
228+
cur int
229+
size int
230+
indexes []int
231+
vectors [][]int
232+
}
233+
234+
func Constructor(v1 []int, v2 []int) *ZigzagIterator {
235+
return &ZigzagIterator{
236+
cur: 0,
237+
size: 2,
238+
indexes: []int{0, 0},
239+
vectors: [][]int{v1, v2},
240+
}
241+
}
242+
243+
func (this *ZigzagIterator) Next() int {
244+
vector := this.vectors[this.cur]
245+
index := this.indexes[this.cur]
246+
res := vector[index]
247+
this.indexes[this.cur]++
248+
this.cur = (this.cur + 1) % this.size
249+
return res
250+
}
251+
252+
func (this *ZigzagIterator) HasNext() bool {
253+
start := this.cur
254+
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
255+
this.cur = (this.cur + 1) % this.size
256+
if start == this.cur {
257+
return false
258+
}
259+
}
260+
return true
261+
}
262+
```
263+
224264
<!-- tabs:end -->
225265

226266
<!-- solution:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type ZigzagIterator struct {
2+
cur int
3+
size int
4+
indexes []int
5+
vectors [][]int
6+
}
7+
8+
func Constructor(v1 []int, v2 []int) *ZigzagIterator {
9+
return &ZigzagIterator{
10+
cur: 0,
11+
size: 2,
12+
indexes: []int{0, 0},
13+
vectors: [][]int{v1, v2},
14+
}
15+
}
16+
17+
func (this *ZigzagIterator) Next() int {
18+
vector := this.vectors[this.cur]
19+
index := this.indexes[this.cur]
20+
res := vector[index]
21+
this.indexes[this.cur]++
22+
this.cur = (this.cur + 1) % this.size
23+
return res
24+
}
25+
26+
func (this *ZigzagIterator) HasNext() bool {
27+
start := this.cur
28+
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
29+
this.cur = (this.cur + 1) % this.size
30+
if start == this.cur {
31+
return false
32+
}
33+
}
34+
return true
35+
}

0 commit comments

Comments
 (0)