File tree Expand file tree Collapse file tree 3 files changed +115
-0
lines changed
solution/0200-0299/0281.Zigzag Iterator Expand file tree Collapse file tree 3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,46 @@ impl ZigzagIterator {
207
207
}
208
208
```
209
209
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
+
210
250
<!-- tabs:end -->
211
251
212
252
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -221,6 +221,46 @@ impl ZigzagIterator {
221
221
}
222
222
```
223
223
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
+
224
264
<!-- tabs:end -->
225
265
226
266
<!-- solution:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments