Skip to content

Commit 2d63f55

Browse files
authored
feat: add solutions to lc problem: No.0281 (#4485)
No.0281.Zigzag Iterator
1 parent a1a4b07 commit 2d63f55

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

Lines changed: 48 additions & 0 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

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,54 @@ public class ZigzagIterator {
163163
*/
164164
```
165165

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+
166214
#### Rust
167215

168216
```rust
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
*/

0 commit comments

Comments
 (0)