|
46 | 46 |
|
47 | 47 | ## Solutions
|
48 | 48 |
|
| 49 | +**Solution 1: Hash Table or Array** |
| 50 | + |
| 51 | +We can use two hash tables or arrays $s1$ and $s2$ to record the elements that appear in the two arrays respectively. |
| 52 | + |
| 53 | +Next, we create an array $ans$ of length $2$, where $ans[0]$ represents the number of elements in $nums1$ that appear in $s2$, and $ans[1]$ represents the number of elements in $nums2$ that appear in $s1$. |
| 54 | + |
| 55 | +Then, we traverse each element $x$ in the array $nums1$. If $x$ has appeared in $s2$, we increment $ans[0]$. After that, we traverse each element $x$ in the array $nums2$. If $x$ has appeared in $s1$, we increment $ans[1]$. |
| 56 | + |
| 57 | +Finally, we return the array $ans$. |
| 58 | + |
| 59 | +The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Here, $n$ and $m$ are the lengths of the arrays $nums1$ and $nums2$ respectively. |
| 60 | + |
49 | 61 | <!-- tabs:start -->
|
50 | 62 |
|
51 | 63 | ### **Python3**
|
52 | 64 |
|
53 | 65 | ```python
|
54 |
| - |
| 66 | +class Solution: |
| 67 | + def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: |
| 68 | + s1, s2 = set(nums1), set(nums2) |
| 69 | + return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)] |
55 | 70 | ```
|
56 | 71 |
|
57 | 72 | ### **Java**
|
58 | 73 |
|
59 | 74 | ```java
|
60 |
| - |
| 75 | +class Solution { |
| 76 | + public int[] findIntersectionValues(int[] nums1, int[] nums2) { |
| 77 | + int[] s1 = new int[101]; |
| 78 | + int[] s2 = new int[101]; |
| 79 | + for (int x : nums1) { |
| 80 | + s1[x] = 1; |
| 81 | + } |
| 82 | + for (int x : nums2) { |
| 83 | + s2[x] = 1; |
| 84 | + } |
| 85 | + int[] ans = new int[2]; |
| 86 | + for (int x : nums1) { |
| 87 | + ans[0] += s2[x]; |
| 88 | + } |
| 89 | + for (int x : nums2) { |
| 90 | + ans[1] += s1[x]; |
| 91 | + } |
| 92 | + return ans; |
| 93 | + } |
| 94 | +} |
61 | 95 | ```
|
62 | 96 |
|
63 | 97 | ### **C++**
|
64 | 98 |
|
65 | 99 | ```cpp
|
66 |
| - |
| 100 | +class Solution { |
| 101 | +public: |
| 102 | + vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) { |
| 103 | + int s1[101]{}; |
| 104 | + int s2[101]{}; |
| 105 | + for (int& x : nums1) { |
| 106 | + s1[x] = 1; |
| 107 | + } |
| 108 | + for (int& x : nums2) { |
| 109 | + s2[x] = 1; |
| 110 | + } |
| 111 | + vector<int> ans(2); |
| 112 | + for (int& x : nums1) { |
| 113 | + ans[0] += s2[x]; |
| 114 | + } |
| 115 | + for (int& x : nums2) { |
| 116 | + ans[1] += s1[x]; |
| 117 | + } |
| 118 | + return ans; |
| 119 | + } |
| 120 | +}; |
67 | 121 | ```
|
68 | 122 |
|
69 | 123 | ### **Go**
|
70 | 124 |
|
71 | 125 | ```go
|
| 126 | +func findIntersectionValues(nums1 []int, nums2 []int) []int { |
| 127 | + s1 := [101]int{} |
| 128 | + s2 := [101]int{} |
| 129 | + for _, x := range nums1 { |
| 130 | + s1[x] = 1 |
| 131 | + } |
| 132 | + for _, x := range nums2 { |
| 133 | + s2[x] = 1 |
| 134 | + } |
| 135 | + ans := make([]int, 2) |
| 136 | + for _, x := range nums1 { |
| 137 | + ans[0] += s2[x] |
| 138 | + } |
| 139 | + for _, x := range nums2 { |
| 140 | + ans[1] += s1[x] |
| 141 | + } |
| 142 | + return ans |
| 143 | +} |
| 144 | +``` |
72 | 145 |
|
| 146 | +### **TypeScript** |
| 147 | + |
| 148 | +```ts |
| 149 | +function findIntersectionValues(nums1: number[], nums2: number[]): number[] { |
| 150 | + const s1: number[] = Array(101).fill(0); |
| 151 | + const s2: number[] = Array(101).fill(0); |
| 152 | + for (const x of nums1) { |
| 153 | + s1[x] = 1; |
| 154 | + } |
| 155 | + for (const x of nums2) { |
| 156 | + s2[x] = 1; |
| 157 | + } |
| 158 | + const ans: number[] = Array(2).fill(0); |
| 159 | + for (const x of nums1) { |
| 160 | + ans[0] += s2[x]; |
| 161 | + } |
| 162 | + for (const x of nums2) { |
| 163 | + ans[1] += s1[x]; |
| 164 | + } |
| 165 | + return ans; |
| 166 | +} |
73 | 167 | ```
|
74 | 168 |
|
75 | 169 | ### **...**
|
|
0 commit comments