HTMLify
LeetCode - Two Out of Three - Go
Views: 324 | Author: abh
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 | func contain(nums []int, target int) bool { for _, n := range nums { if n == target { return true } } return false } func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) []int { var distincs []int all := [][]int{nums1, nums2, nums3} for _, nums := range all { for _, n := range nums { if !contain(distincs, n) { distincs = append(distincs, n) } } } var ans []int for _, n := range distincs { fc := 0 if contain(nums1, n) { fc++ } if contain(nums2, n) { fc++ } if contain(nums3, n) { fc++ } if fc > 1 { ans = append(ans, n) } } return ans } |