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
| // @Title: 较大分组的位置 (Positions of Large Groups)
// @Author: 15816537946@163.com
// @Date: 2021-01-05 17:04:12
// @Runtime: 0 ms
// @Memory: 2.6 MB
/*
func largeGroupPositions(s string) [][]int {
n := len(s)
if n < 3 {
return nil
}
var ret [][]int
cursor := s[0]
t := []int{0}
for i := range s {
if s[i] == cursor {
if len(t) == 1 {
t = append(t, i)
} else {
t[1] = i
}
} else {
if len(t) == 2 && t[1]-t[0] >= 2 {
ret = append(ret, t)
}
t = []int{i}
cursor = s[i]
}
}
if len(t) == 2 && t[1]-t[0] >= 2 {
ret = append(ret, t)
}
return ret
}
*/
func largeGroupPositions(s string) (ans [][]int) {
cnt := 1
for i := range s {
if i == len(s)-1 || s[i] != s[i+1] {
if cnt >= 3 {
ans = append(ans, []int{i - cnt + 1, i})
}
cnt = 1
} else {
cnt++
}
}
return
}
|