utils.go
1.4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package utils
import (
"strconv"
"github.com/shopspring/decimal"
)
// Intersect 返回两个数组的交集
func Intersect(nums1 []int64, nums2 []int64) []int64 {
if len(nums1) > len(nums2) {
return Intersect(nums2, nums1)
}
m := map[int64]int64{}
for _, num := range nums1 {
m[num]++
}
var intersection []int64
for _, num := range nums2 {
if m[num] > 0 {
intersection = append(intersection, num)
m[num]--
}
}
return intersection
}
////求交集
//func intersect(slice1, slice2 []int64) []int64 {
// m := make(map[int64]int)
// nn := make([]int64, 0)
// for _, v := range slice1 {
// m[v]++
// }
//
// for _, v := range slice2 {
// times, _ := m[v]
// if times == 1 {
// nn = append(nn, v)
// }
// }
// return nn
//}
// Difference 求差集 slice1-并集
func Difference(slice1, slice2 []int64) []int64 {
m := make(map[int64]int)
nn := make([]int64, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v]++
}
for _, value := range slice1 {
times, _ := m[value]
if times == 0 {
nn = append(nn, value)
}
}
return nn
}
func SliceAtoi(sa []string) ([]int64, error) {
si := make([]int64, 0, len(sa))
for _, a := range sa {
i, err := strconv.ParseInt(a, 10, 64)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
func Round(value float64, places int32) float64 {
quantity := decimal.NewFromFloat(value)
d := quantity.Round(places)
rsp, _ := d.Float64()
return rsp
}