File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed
solution/0200-0299/0281.Zigzag Iterator Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change @@ -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
Original file line number Diff line number Diff line change @@ -163,6 +163,54 @@ public class ZigzagIterator {
163
163
*/
164
164
```
165
165
166
+ #### Go
167
+
168
+ ``` go
169
+ type ZigzagIterator struct {
170
+ cur int
171
+ size int
172
+ indexes []int
173
+ vectors [][]int
174
+ }
175
+
176
+ func Constructor (v1 , v2 []int ) *ZigzagIterator {
177
+ return &ZigzagIterator{
178
+ cur: 0 ,
179
+ size: 2 ,
180
+ indexes: []int {0 , 0 },
181
+ vectors: [][]int {v1, v2},
182
+ }
183
+ }
184
+
185
+ func (this *ZigzagIterator ) next () int {
186
+ vector := this.vectors [this.cur ]
187
+ index := this.indexes [this.cur ]
188
+ res := vector[index]
189
+ this.indexes [this.cur ]++
190
+ this.cur = (this.cur + 1 ) % this.size
191
+ return res
192
+ }
193
+
194
+ func (this *ZigzagIterator ) hasNext () bool {
195
+ start := this.cur
196
+ for this.indexes [this.cur ] == len (this.vectors [this.cur ]) {
197
+ this.cur = (this.cur + 1 ) % this.size
198
+ if start == this.cur {
199
+ return false
200
+ }
201
+ }
202
+ return true
203
+ }
204
+
205
+ /* *
206
+ * Your ZigzagIterator object will be instantiated and called as such:
207
+ * obj := Constructor(param_1, param_2);
208
+ * for obj.hasNext() {
209
+ * ans = append(ans, obj.next())
210
+ * }
211
+ */
212
+ ```
213
+
166
214
#### Rust
167
215
168
216
``` rust
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 , 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
+ }
36
+
37
+ /**
38
+ * Your ZigzagIterator object will be instantiated and called as such:
39
+ * obj := Constructor(param_1, param_2);
40
+ * for obj.hasNext() {
41
+ * ans = append(ans, obj.next())
42
+ * }
43
+ */
You can’t perform that action at this time.
0 commit comments