Skip to content

Commit 99b39a1

Browse files
committed
feat: add solutions to lc problem: No.2248
No.2248.Intersection of Multiple Arrays
1 parent bd4b0de commit 99b39a1

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

solution/2200-2299/2248.Intersection of Multiple Arrays/README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ nums[0] = [<em><strong>3</strong></em>,1,2,<em><strong>4</strong></em>,5],nums
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
class Solution:
53+
def intersection(self, nums: List[List[int]]) -> List[int]:
54+
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]
5360
```
5461

5562
### **Java**
@@ -91,6 +98,46 @@ function intersection(nums: number[][]): number[] {
9198
};
9299
```
93100

101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
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();
112+
vector<int> ans;
113+
for (int i = 1; i < 1001; ++i)
114+
if (cnt[i] == n)
115+
ans.push_back(i);
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```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]++
129+
}
130+
}
131+
var ans []int
132+
for i, v := range cnt {
133+
if v == len(nums) {
134+
ans = append(ans, i)
135+
}
136+
}
137+
return ans
138+
}
139+
```
140+
94141
### **...**
95142

96143
```

solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ There does not exist any integer present both in nums[0] and nums[1], so we retu
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def intersection(self, nums: List[List[int]]) -> List[int]:
46+
cnt = [0] * 1001
47+
for num in nums:
48+
for v in num:
49+
cnt[v] += 1
50+
n = len(nums)
51+
return [i for i, v in enumerate(cnt) if v == n]
4552
```
4653

4754
### **Java**
@@ -81,6 +88,46 @@ function intersection(nums: number[][]): number[] {
8188
};
8289
```
8390

91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<int> intersection(vector<vector<int>>& nums) {
97+
vector<int> cnt(1001);
98+
for (auto& num : nums)
99+
for (int v : num)
100+
++cnt[v];
101+
int n = nums.size();
102+
vector<int> ans;
103+
for (int i = 1; i < 1001; ++i)
104+
if (cnt[i] == n)
105+
ans.push_back(i);
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func intersection(nums [][]int) []int {
115+
cnt := make([]int, 1001)
116+
for _, num := range nums {
117+
for _, v := range num {
118+
cnt[v]++
119+
}
120+
}
121+
var ans []int
122+
for i, v := range cnt {
123+
if v == len(nums) {
124+
ans = append(ans, i)
125+
}
126+
}
127+
return ans
128+
}
129+
```
130+
84131
### **...**
85132

86133
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> intersection(vector<vector<int>>& nums) {
4+
vector<int> cnt(1001);
5+
for (auto& num : nums)
6+
for (int v : num)
7+
++cnt[v];
8+
int n = nums.size();
9+
vector<int> ans;
10+
for (int i = 1; i < 1001; ++i)
11+
if (cnt[i] == n)
12+
ans.push_back(i);
13+
return ans;
14+
}
15+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func intersection(nums [][]int) []int {
2+
cnt := make([]int, 1001)
3+
for _, num := range nums {
4+
for _, v := range num {
5+
cnt[v]++
6+
}
7+
}
8+
var ans []int
9+
for i, v := range cnt {
10+
if v == len(nums) {
11+
ans = append(ans, i)
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def intersection(self, nums: List[List[int]]) -> List[int]:
3+
cnt = [0] * 1001
4+
for num in nums:
5+
for v in num:
6+
cnt[v] += 1
7+
n = len(nums)
8+
return [i for i, v in enumerate(cnt) if v == n]

0 commit comments

Comments
 (0)