-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
49 lines (44 loc) · 899 Bytes
/
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
type pair struct {
c byte
x int
}
type StringIterator struct {
d []pair
p int
}
func Constructor(compressedString string) StringIterator {
n := len(compressedString)
i := 0
d := []pair{}
for i < n {
c := compressedString[i]
x := 0
i++
for i < n && compressedString[i] >= '0' && compressedString[i] <= '9' {
x = x*10 + int(compressedString[i]-'0')
i++
}
d = append(d, pair{c, x})
}
return StringIterator{d, 0}
}
func (this *StringIterator) Next() byte {
if !this.HasNext() {
return ' '
}
ans := this.d[this.p].c
this.d[this.p].x--
if this.d[this.p].x == 0 {
this.p++
}
return ans
}
func (this *StringIterator) HasNext() bool {
return this.p < len(this.d) && this.d[this.p].x > 0
}
/**
* Your StringIterator object will be instantiated and called as such:
* obj := Constructor(compressedString);
* param_1 := obj.Next();
* param_2 := obj.HasNext();
*/