classSolution { public: vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2){ unordered_set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()); vector<int> ans(2); for (int t : nums1) { ans[0] += s2.count(t); } for (int t : nums2) { ans[1] += s1.count(t); } return ans; } };
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package main
funcfindIntersectionValues(nums1 []int, nums2 []int) []int { s1, s2 := map[int]int{}, map[int]int{} for _, t := range nums1 { s1[t] = 1 } for _, t := range nums2 { s2[t] = 1 } ans := [2]int{} for _, t := range nums1 { ans[0] += s2[t] } for _, t := range nums2 { ans[1] += s1[t] } return ans[:] }
classSolution { publicint[] findIntersectionValues(int[] nums1, int[] nums2) { HashSet<Integer> s1 = newHashSet<>(), s2 = newHashSet<>(); for (int t : nums1) { s1.add(t); } for (int t : nums2) { s2.add(t); } int[] ans = newint[2]; for (int t : nums1) { ans[0] += s2.contains(t) ? 1 : 0; } for (int t : nums2) { ans[1] += s1.contains(t) ? 1 : 0; } return ans; } }
Python
1 2 3 4 5
from typing importList
classSolution: deffindIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: return [sum(t in nums2 for t in nums1), sum(t in nums1 for t in nums2)]