-
Notifications
You must be signed in to change notification settings - Fork 4
/
int.go
49 lines (47 loc) · 924 Bytes
/
int.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
package watertower
import (
"sort"
)
func intersection(sorted ...[]uint32) []uint32 {
sort.Slice(sorted, func(i, j int) bool {
return len(sorted[i]) < len(sorted[j])
})
var result []uint32
if len(sorted) == 0 || len(sorted[0]) == 0 {
return result
}
cursors := make([]int, len(sorted))
terminate := false
for _, value := range sorted[0] {
needIncrement := false
for i := 1; i < len(sorted); i++ {
found := false
for j := cursors[i]; j < len(sorted[i]); j++ {
valueOfOtherSlice := sorted[i][cursors[i]]
if valueOfOtherSlice < value {
cursors[i] = j + 1
} else if value < valueOfOtherSlice {
needIncrement = true
break
} else {
found = true
break
}
}
if needIncrement {
break
}
if !found {
terminate = true
break
}
}
if terminate {
break
}
if !needIncrement {
result = append(result, value)
}
}
return result
}