Skip to content

Commit b769fb8

Browse files
authored
feat: add solutions to lc problem: No.0349 (doocs#1421)
1 parent e30b1ea commit b769fb8

File tree

7 files changed

+154
-172
lines changed

7 files changed

+154
-172
lines changed

Diff for: solution/0300-0399/0349.Intersection of Two Arrays/README.md

+61-63
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
“哈希表”实现。
41+
**方法一:哈希表或数组**
42+
43+
我们先用哈希表或者一个长度为 $1001$ 的数组 $s$ 记录数组 $nums1$ 中出现的元素,然后遍历数组 $nums2$ 中每个元素,如果元素 $x$ 在 $s$ 中,那么将 $x$ 加入答案,并且从 $s$ 中移除 $x$。
44+
45+
遍历结束后,返回答案数组即可。
46+
47+
时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $nums1$ 和 $nums2$ 的长度。
4248

4349
<!-- tabs:start -->
4450

@@ -49,12 +55,7 @@
4955
```python
5056
class Solution:
5157
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
52-
s = set(nums1)
53-
res = set()
54-
for num in nums2:
55-
if num in s:
56-
res.add(num)
57-
return list(res)
58+
return list(set(nums1) & set(nums2))
5859
```
5960

6061
### **Java**
@@ -64,90 +65,87 @@ class Solution:
6465
```java
6566
class Solution {
6667
public int[] intersection(int[] nums1, int[] nums2) {
67-
Set<Integer> s = new HashSet<>();
68-
for (int num : nums1) {
69-
s.add(num);
68+
boolean[] s = new boolean[1001];
69+
for (int x : nums1) {
70+
s[x] = true;
7071
}
71-
Set<Integer> t = new HashSet<>();
72-
for (int num : nums2) {
73-
if (s.contains(num)) {
74-
t.add(num);
72+
List<Integer> ans = new ArrayList<>();
73+
for (int x : nums2) {
74+
if (s[x]) {
75+
ans.add(x);
76+
s[x] = false;
7577
}
7678
}
77-
int[] res = new int[t.size()];
78-
int i = 0;
79-
for (int num : t) {
80-
res[i++] = num;
81-
}
82-
return res;
79+
return ans.stream().mapToInt(Integer::intValue).toArray();
8380
}
8481
}
8582
```
8683

87-
### **JavaScript**
88-
89-
```js
90-
/**
91-
* @param {number[]} nums1
92-
* @param {number[]} nums2
93-
* @return {number[]}
94-
*/
95-
var intersection = function (nums1, nums2) {
96-
const s = new Set();
97-
for (const num of nums1) {
98-
s.add(num);
99-
}
100-
let res = new Set();
101-
for (const num of nums2) {
102-
if (s.has(num)) {
103-
res.add(num);
104-
}
105-
}
106-
return [...res];
107-
};
108-
```
109-
11084
### **C++**
11185

11286
```cpp
11387
class Solution {
11488
public:
11589
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
116-
unordered_set<int> s;
117-
for (int num : nums1) s.insert(num);
118-
unordered_set<int> t;
119-
vector<int> res;
120-
for (int num : nums2) {
121-
if (s.count(num) && !t.count(num)) {
122-
t.insert(num);
123-
res.push_back(num);
90+
bool s[1001];
91+
memset(s, false, sizeof(s));
92+
for (int x : nums1) {
93+
s[x] = true;
94+
}
95+
vector<int> ans;
96+
for (int x : nums2) {
97+
if (s[x]) {
98+
ans.push_back(x);
99+
s[x] = false;
124100
}
125101
}
126-
return res;
102+
return ans;
127103
}
128104
};
129105
```
130106
131107
### **Go**
132108
133109
```go
134-
func intersection(nums1 []int, nums2 []int) []int {
135-
s := make(map[int]bool)
136-
for _, num := range nums1 {
137-
s[num] = true
110+
func intersection(nums1 []int, nums2 []int) (ans []int) {
111+
s := [1001]bool{}
112+
for _, x := range nums1 {
113+
s[x] = true
138114
}
139-
t := make(map[int]bool)
140-
var res []int
141-
for _, num := range nums2 {
142-
if s[num] && !t[num] {
143-
res = append(res, num)
144-
t[num] = true
115+
for _, x := range nums2 {
116+
if s[x] {
117+
ans = append(ans, x)
118+
s[x] = false
145119
}
146120
}
147-
return res
121+
return
148122
}
149123
```
150124

125+
### **JavaScript**
126+
127+
```js
128+
/**
129+
* @param {number[]} nums1
130+
* @param {number[]} nums2
131+
* @return {number[]}
132+
*/
133+
var intersection = function (nums1, nums2) {
134+
const s = Array(1001).fill(false);
135+
for (const x of nums1) {
136+
s[x] = true;
137+
}
138+
const ans = [];
139+
for (const x of nums2) {
140+
if (s[x]) {
141+
ans.push(x);
142+
s[x] = false;
143+
}
144+
}
145+
return ans;
146+
};
147+
```
148+
151149
### **PHP**
152150

153151
```php

Diff for: solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md

+54-62
Original file line numberDiff line numberDiff line change
@@ -39,103 +39,95 @@
3939
```python
4040
class Solution:
4141
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
42-
s = set(nums1)
43-
res = set()
44-
for num in nums2:
45-
if num in s:
46-
res.add(num)
47-
return list(res)
42+
return list(set(nums1) & set(nums2))
4843
```
4944

5045
### **Java**
5146

5247
```java
5348
class Solution {
5449
public int[] intersection(int[] nums1, int[] nums2) {
55-
Set<Integer> s = new HashSet<>();
56-
for (int num : nums1) {
57-
s.add(num);
50+
boolean[] s = new boolean[1001];
51+
for (int x : nums1) {
52+
s[x] = true;
5853
}
59-
Set<Integer> t = new HashSet<>();
60-
for (int num : nums2) {
61-
if (s.contains(num)) {
62-
t.add(num);
54+
List<Integer> ans = new ArrayList<>();
55+
for (int x : nums2) {
56+
if (s[x]) {
57+
ans.add(x);
58+
s[x] = false;
6359
}
6460
}
65-
int[] res = new int[t.size()];
66-
int i = 0;
67-
for (int num : t) {
68-
res[i++] = num;
69-
}
70-
return res;
61+
return ans.stream().mapToInt(Integer::intValue).toArray();
7162
}
7263
}
7364
```
7465

75-
### **JavaScript**
76-
77-
```js
78-
/**
79-
* @param {number[]} nums1
80-
* @param {number[]} nums2
81-
* @return {number[]}
82-
*/
83-
var intersection = function (nums1, nums2) {
84-
const s = new Set();
85-
for (const num of nums1) {
86-
s.add(num);
87-
}
88-
let res = new Set();
89-
for (const num of nums2) {
90-
if (s.has(num)) {
91-
res.add(num);
92-
}
93-
}
94-
return [...res];
95-
};
96-
```
97-
9866
### **C++**
9967

10068
```cpp
10169
class Solution {
10270
public:
10371
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
104-
unordered_set<int> s;
105-
for (int num : nums1) s.insert(num);
106-
unordered_set<int> t;
107-
vector<int> res;
108-
for (int num : nums2) {
109-
if (s.count(num) && !t.count(num)) {
110-
t.insert(num);
111-
res.push_back(num);
72+
bool s[1001];
73+
memset(s, false, sizeof(s));
74+
for (int x : nums1) {
75+
s[x] = true;
76+
}
77+
vector<int> ans;
78+
for (int x : nums2) {
79+
if (s[x]) {
80+
ans.push_back(x);
81+
s[x] = false;
11282
}
11383
}
114-
return res;
84+
return ans;
11585
}
11686
};
11787
```
11888
11989
### **Go**
12090
12191
```go
122-
func intersection(nums1 []int, nums2 []int) []int {
123-
s := make(map[int]bool)
124-
for _, num := range nums1 {
125-
s[num] = true
92+
func intersection(nums1 []int, nums2 []int) (ans []int) {
93+
s := [1001]bool{}
94+
for _, x := range nums1 {
95+
s[x] = true
12696
}
127-
t := make(map[int]bool)
128-
var res []int
129-
for _, num := range nums2 {
130-
if s[num] && !t[num] {
131-
res = append(res, num)
132-
t[num] = true
97+
for _, x := range nums2 {
98+
if s[x] {
99+
ans = append(ans, x)
100+
s[x] = false
133101
}
134102
}
135-
return res
103+
return
136104
}
137105
```
138106

107+
### **JavaScript**
108+
109+
```js
110+
/**
111+
* @param {number[]} nums1
112+
* @param {number[]} nums2
113+
* @return {number[]}
114+
*/
115+
var intersection = function (nums1, nums2) {
116+
const s = Array(1001).fill(false);
117+
for (const x of nums1) {
118+
s[x] = true;
119+
}
120+
const ans = [];
121+
for (const x of nums2) {
122+
if (s[x]) {
123+
ans.push(x);
124+
s[x] = false;
125+
}
126+
}
127+
return ans;
128+
};
129+
```
130+
139131
### **PHP**
140132

141133
```php
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution {
22
public:
33
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
4-
unordered_set<int> s;
5-
for (int num : nums1) s.insert(num);
6-
unordered_set<int> t;
7-
vector<int> res;
8-
for (int num : nums2) {
9-
if (s.count(num) && !t.count(num)) {
10-
t.insert(num);
11-
res.push_back(num);
4+
bool s[1001];
5+
memset(s, false, sizeof(s));
6+
for (int x : nums1) {
7+
s[x] = true;
8+
}
9+
vector<int> ans;
10+
for (int x : nums2) {
11+
if (s[x]) {
12+
ans.push_back(x);
13+
s[x] = false;
1214
}
1315
}
14-
return res;
16+
return ans;
1517
}
1618
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
func intersection(nums1 []int, nums2 []int) []int {
2-
s := make(map[int]bool)
3-
for _, num := range nums1 {
4-
s[num] = true
1+
func intersection(nums1 []int, nums2 []int) (ans []int) {
2+
s := [1001]bool{}
3+
for _, x := range nums1 {
4+
s[x] = true
55
}
6-
t := make(map[int]bool)
7-
var res []int
8-
for _, num := range nums2 {
9-
if s[num] && !t[num] {
10-
res = append(res, num)
11-
t[num] = true
6+
for _, x := range nums2 {
7+
if s[x] {
8+
ans = append(ans, x)
9+
s[x] = false
1210
}
1311
}
14-
return res
12+
return
1513
}

0 commit comments

Comments
 (0)