@@ -149,6 +149,54 @@ public class ZigzagIterator {
149
149
*/
150
150
```
151
151
152
+ #### Go
153
+
154
+ ``` go
155
+ type ZigzagIterator struct {
156
+ cur int
157
+ size int
158
+ indexes []int
159
+ vectors [][]int
160
+ }
161
+
162
+ func Constructor (v1 , v2 []int ) *ZigzagIterator {
163
+ return &ZigzagIterator{
164
+ cur: 0 ,
165
+ size: 2 ,
166
+ indexes: []int {0 , 0 },
167
+ vectors: [][]int {v1, v2},
168
+ }
169
+ }
170
+
171
+ func (this *ZigzagIterator ) next () int {
172
+ vector := this.vectors [this.cur ]
173
+ index := this.indexes [this.cur ]
174
+ res := vector[index]
175
+ this.indexes [this.cur ]++
176
+ this.cur = (this.cur + 1 ) % this.size
177
+ return res
178
+ }
179
+
180
+ func (this *ZigzagIterator ) hasNext () bool {
181
+ start := this.cur
182
+ for this.indexes [this.cur ] == len (this.vectors [this.cur ]) {
183
+ this.cur = (this.cur + 1 ) % this.size
184
+ if start == this.cur {
185
+ return false
186
+ }
187
+ }
188
+ return true
189
+ }
190
+
191
+ /* *
192
+ * Your ZigzagIterator object will be instantiated and called as such:
193
+ * obj := Constructor(param_1, param_2);
194
+ * for obj.hasNext() {
195
+ * ans = append(ans, obj.next())
196
+ * }
197
+ */
198
+ ```
199
+
152
200
#### Rust
153
201
154
202
``` rust
@@ -207,46 +255,6 @@ impl ZigzagIterator {
207
255
}
208
256
```
209
257
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
-
250
258
<!-- tabs:end -->
251
259
252
260
<!-- solution:end -->
0 commit comments