-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
66 lines (56 loc) · 1 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package lc0284
type PeekingIterator struct {
iter *Iterator
nextValue int
}
func Constructor(iter *Iterator) *PeekingIterator {
return &PeekingIterator{
iter: iter,
nextValue: 0,
}
}
func (i *PeekingIterator) hasNext() bool {
if i.nextValue > 0 {
return true
}
if !i.iter.hasNext() {
return false
}
i.nextValue = i.iter.next()
return true
}
func (i *PeekingIterator) next() int {
value := i.peek()
i.nextValue = 0
return value
}
func (i *PeekingIterator) peek() int {
if !i.hasNext() {
panic("no more elements")
}
return i.nextValue
}
// ============================================================================
// Test Data
//
type Iterator struct {
arr []int
cursor int
}
func NewIterator(arr []int) *Iterator {
return &Iterator{
arr: arr,
cursor: 0,
}
}
func (i *Iterator) hasNext() bool {
return i.cursor < len(i.arr)
}
func (i *Iterator) next() int {
if !i.hasNext() {
panic("no more elements")
}
value := i.arr[i.cursor]
i.cursor++
return value
}