又用了哈希表,又用了数学计算,看题解有个位运算看不太懂
func findErrorNums(nums []int) []int {
m := make(map[int]struct{}, len(nums))
pSum := (1+len(nums))*len(nums)/2
ret := []int{}
sum := 0
for _, v := range nums {
sum+=v
if _, ok := m[v]; ok {
ret = append(ret, v)
}
m[v] = struct{}{}
}
// fmt.Print(sum, pSum)
ret = append(ret, ret[0] - (sum - pSum))
return ret
}
标签:nums,int,sum,ret,pSum,645,len,集合,leetcode
From: https://www.cnblogs.com/wudanyang/p/17023752.html