@@ -42,6 +42,12 @@ nums[0] = [<em><strong>3</strong></em>,1,2,<em><strong>4</strong></em>,5],nums
42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
+ ** 方法一:计数**
46
+
47
+ 遍历数组 ` nums ` ,对于每个数组 ` arr ` ,统计数组 ` arr ` 中每个数字出现的次数,然后遍历计数数组,统计出现次数等于数组 ` nums ` 的长度的数字,即为答案。
48
+
49
+ 时间复杂度 $O(N)$,空间复杂度 $O(1000)$。其中 $N$ 为数组 ` nums ` 中数字的总数。
50
+
45
51
<!-- tabs:start -->
46
52
47
53
### ** Python3**
@@ -52,11 +58,24 @@ nums[0] = [<em><strong>3</strong></em>,1,2,<em><strong>4</strong></em>,5],nums
52
58
class Solution :
53
59
def intersection (self , nums : List[List[int ]]) -> List[int ]:
54
60
cnt = [0 ] * 1001
55
- for num in nums:
56
- for v in num:
57
- cnt[v] += 1
58
- n = len (nums)
59
- return [i for i, v in enumerate (cnt) if v == n]
61
+ for arr in nums:
62
+ for x in arr:
63
+ cnt[x] += 1
64
+ return [x for x, v in enumerate (cnt) if v == len (nums)]
65
+ ```
66
+
67
+ ``` python
68
+ class Solution :
69
+ def intersection (self , nums : List[List[int ]]) -> List[int ]:
70
+ cnt = Counter()
71
+ ans = []
72
+ for arr in nums:
73
+ for x in arr:
74
+ cnt[x] += 1
75
+ if cnt[x] == len (nums):
76
+ ans.append(x)
77
+ ans.sort()
78
+ return ans
60
79
```
61
80
62
81
### ** Java**
@@ -67,34 +86,37 @@ class Solution:
67
86
class Solution {
68
87
public List<Integer > intersection (int [][] nums ) {
69
88
int [] cnt = new int [1001 ];
70
- for (int [] num : nums) {
71
- for (int i : num ) {
72
- cnt[i] ++ ;
89
+ for (var arr : nums) {
90
+ for (int x : arr ) {
91
+ ++ cnt[x] ;
73
92
}
74
93
}
75
94
List<Integer > ans = new ArrayList<> ();
76
- for (int i = 1 ; i <= 1000 ; i ++ ) {
77
- if (cnt[i ] == nums. length) {
78
- ans. add(i );
95
+ for (int x = 0 ; x < 1001 ; ++ x ) {
96
+ if (cnt[x ] == nums. length) {
97
+ ans. add(x );
79
98
}
80
99
}
81
100
return ans;
82
101
}
83
102
}
84
103
```
85
104
86
- ### ** TypeScript**
87
-
88
- ``` ts
89
- function intersection(nums : number [][]): number [] {
90
- const n = nums .length ;
91
- let ans = nums [0 ];
92
- for (let i = 1 ; i < n && ans .length ; i ++ ) {
93
- const cur = new Set (nums [i ]);
94
- // get intersect
95
- ans = ans .filter (v => cur .has (v ));
105
+ ``` java
106
+ class Solution {
107
+ public List<Integer > intersection (int [][] nums ) {
108
+ Map<Integer , Integer > cnt = new HashMap<> ();
109
+ List<Integer > ans = new ArrayList<> ();
110
+ for (var arr : nums) {
111
+ for (int x : arr) {
112
+ if (cnt. merge(x, 1 , Integer :: sum) == nums. length) {
113
+ ans. add(x);
114
+ }
115
+ }
116
+ }
117
+ Collections . sort(ans);
118
+ return ans;
96
119
}
97
- return ans .sort ((a , b ) => a - b );
98
120
}
99
121
```
100
122
@@ -104,15 +126,37 @@ function intersection(nums: number[][]): number[] {
104
126
class Solution {
105
127
public:
106
128
vector<int > intersection(vector<vector<int >>& nums) {
107
- vector<int > cnt(1001);
108
- for (auto& num : nums)
109
- for (int v : num)
110
- ++cnt[ v] ;
111
- int n = nums.size();
129
+ int cnt[ 1001] {};
130
+ for (auto& arr : nums) {
131
+ for (int& x : arr) {
132
+ ++cnt[ x] ;
133
+ }
134
+ }
135
+ vector<int > ans;
136
+ for (int x = 0; x < 1001; ++x) {
137
+ if (cnt[ x] == nums.size()) {
138
+ ans.push_back(x);
139
+ }
140
+ }
141
+ return ans;
142
+ }
143
+ };
144
+ ```
145
+
146
+ ```cpp
147
+ class Solution {
148
+ public:
149
+ vector<int> intersection(vector<vector<int>>& nums) {
150
+ unordered_map<int, int> cnt;
112
151
vector<int> ans;
113
- for (int i = 1; i < 1001; ++i)
114
- if (cnt[ i] == n)
115
- ans.push_back(i);
152
+ for (auto& arr : nums) {
153
+ for (int& x : arr) {
154
+ if (++cnt[x] == nums.size()) {
155
+ ans.push_back(x);
156
+ }
157
+ }
158
+ }
159
+ sort(ans.begin(), ans.end());
116
160
return ans;
117
161
}
118
162
};
@@ -121,20 +165,71 @@ public:
121
165
### ** Go**
122
166
123
167
``` go
124
- func intersection(nums [][]int) []int {
125
- cnt := make([ ]int, 1001)
126
- for _, num := range nums {
127
- for _, v := range num {
128
- cnt[v ]++
168
+ func intersection (nums [][]int ) ( ans []int ) {
169
+ cnt := [ 1001 ]int {}
170
+ for _ , arr := range nums {
171
+ for _ , x := range arr {
172
+ cnt[x ]++
129
173
}
130
174
}
131
- var ans []int
132
- for i, v := range cnt {
175
+ for x , v := range cnt {
133
176
if v == len (nums) {
134
- ans = append(ans, i )
177
+ ans = append (ans, x )
135
178
}
136
179
}
137
- return ans
180
+ return
181
+ }
182
+ ```
183
+
184
+ ``` go
185
+ func intersection (nums [][]int ) (ans []int ) {
186
+ cnt := map [int ]int {}
187
+ for _ , arr := range nums {
188
+ for _ , x := range arr {
189
+ cnt[x]++
190
+ if cnt[x] == len (nums) {
191
+ ans = append (ans, x)
192
+ }
193
+ }
194
+ }
195
+ sort.Ints (ans)
196
+ return
197
+ }
198
+ ```
199
+
200
+ ### ** TypeScript**
201
+
202
+ ``` ts
203
+ function intersection(nums : number [][]): number [] {
204
+ const cnt = new Array (1001 ).fill (0 );
205
+ for (const arr of nums ) {
206
+ for (const x of arr ) {
207
+ cnt [x ]++ ;
208
+ }
209
+ }
210
+ const ans: number [] = [];
211
+ for (let x = 0 ; x < 1001 ; x ++ ) {
212
+ if (cnt [x ] === nums .length ) {
213
+ ans .push (x );
214
+ }
215
+ }
216
+ return ans ;
217
+ }
218
+ ```
219
+
220
+ ``` ts
221
+ function intersection(nums : number [][]): number [] {
222
+ const cnt = new Array (1001 ).fill (0 );
223
+ const ans: number [] = [];
224
+ for (const arr of nums ) {
225
+ for (const x of arr ) {
226
+ if (++ cnt [x ] == nums .length ) {
227
+ ans .push (x );
228
+ }
229
+ }
230
+ }
231
+ ans .sort ((a , b ) => a - b );
232
+ return ans ;
138
233
}
139
234
```
140
235
0 commit comments