Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions solution/0200-0299/0281.Zigzag Iterator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,54 @@ public class ZigzagIterator {
*/
```

#### Go

```go
type ZigzagIterator struct {
cur int
size int
indexes []int
vectors [][]int
}

func Constructor(v1, v2 []int) *ZigzagIterator {
return &ZigzagIterator{
cur: 0,
size: 2,
indexes: []int{0, 0},
vectors: [][]int{v1, v2},
}
}

func (this *ZigzagIterator) next() int {
vector := this.vectors[this.cur]
index := this.indexes[this.cur]
res := vector[index]
this.indexes[this.cur]++
this.cur = (this.cur + 1) % this.size
return res
}

func (this *ZigzagIterator) hasNext() bool {
start := this.cur
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
this.cur = (this.cur + 1) % this.size
if start == this.cur {
return false
}
}
return true
}

/**
* Your ZigzagIterator object will be instantiated and called as such:
* obj := Constructor(param_1, param_2);
* for obj.hasNext() {
* ans = append(ans, obj.next())
* }
*/
```

#### Rust

```rust
Expand Down
48 changes: 48 additions & 0 deletions solution/0200-0299/0281.Zigzag Iterator/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,54 @@ public class ZigzagIterator {
*/
```

#### Go

```go
type ZigzagIterator struct {
cur int
size int
indexes []int
vectors [][]int
}

func Constructor(v1, v2 []int) *ZigzagIterator {
return &ZigzagIterator{
cur: 0,
size: 2,
indexes: []int{0, 0},
vectors: [][]int{v1, v2},
}
}

func (this *ZigzagIterator) next() int {
vector := this.vectors[this.cur]
index := this.indexes[this.cur]
res := vector[index]
this.indexes[this.cur]++
this.cur = (this.cur + 1) % this.size
return res
}

func (this *ZigzagIterator) hasNext() bool {
start := this.cur
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
this.cur = (this.cur + 1) % this.size
if start == this.cur {
return false
}
}
return true
}

/**
* Your ZigzagIterator object will be instantiated and called as such:
* obj := Constructor(param_1, param_2);
* for obj.hasNext() {
* ans = append(ans, obj.next())
* }
*/
```

#### Rust

```rust
Expand Down
43 changes: 43 additions & 0 deletions solution/0200-0299/0281.Zigzag Iterator/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
type ZigzagIterator struct {
cur int
size int
indexes []int
vectors [][]int
}

func Constructor(v1, v2 []int) *ZigzagIterator {
return &ZigzagIterator{
cur: 0,
size: 2,
indexes: []int{0, 0},
vectors: [][]int{v1, v2},
}
}

func (this *ZigzagIterator) next() int {
vector := this.vectors[this.cur]
index := this.indexes[this.cur]
res := vector[index]
this.indexes[this.cur]++
this.cur = (this.cur + 1) % this.size
return res
}

func (this *ZigzagIterator) hasNext() bool {
start := this.cur
for this.indexes[this.cur] == len(this.vectors[this.cur]) {
this.cur = (this.cur + 1) % this.size
if start == this.cur {
return false
}
}
return true
}

/**
* Your ZigzagIterator object will be instantiated and called as such:
* obj := Constructor(param_1, param_2);
* for obj.hasNext() {
* ans = append(ans, obj.next())
* }
*/