Skip to content

Commit 429b733

Browse files
authored
Update README.md
1 parent c1d7e0d commit 429b733

File tree

1 file changed

+48
-40
lines changed
  • solution/0200-0299/0281.Zigzag Iterator

1 file changed

+48
-40
lines changed

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

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,54 @@ public class ZigzagIterator {
149149
*/
150150
```
151151

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+
152200
#### Rust
153201

154202
```rust
@@ -207,46 +255,6 @@ impl ZigzagIterator {
207255
}
208256
```
209257

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-
250258
<!-- tabs:end -->
251259

252260
<!-- solution:end -->

0 commit comments

Comments
 (0)