Skip to content

Commit 3e85e3d

Browse files
committed
feat: add solutions to lc problems: No.0349,0350
1 parent 2889ef7 commit 3e85e3d

File tree

13 files changed

+270
-72
lines changed

13 files changed

+270
-72
lines changed

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

+42-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
“哈希表”实现。
39+
3840
<!-- tabs:start -->
3941

4042
### **Python3**
@@ -44,8 +46,12 @@
4446
```python
4547
class Solution:
4648
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
47-
s1, s2 = set(nums1), set(nums2)
48-
return list(s1 & s2)
49+
s = set(nums1)
50+
res = set()
51+
for num in nums2:
52+
if num in s:
53+
res.add(num)
54+
return list(res)
4955
```
5056

5157
### **Java**
@@ -55,25 +61,47 @@ class Solution:
5561
```java
5662
class Solution {
5763
public int[] intersection(int[] nums1, int[] nums2) {
58-
Set<Integer> s1 = transfer(nums1);
59-
Set<Integer> s2 = transfer(nums2);
60-
s1.retainAll(s2);
61-
int[] output = new int[s1.size()];
64+
Set<Integer> s = new HashSet<>();
65+
for (int num : nums1) {
66+
s.add(num);
67+
}
68+
Set<Integer> res = new HashSet<>();
69+
for (int num : nums2) {
70+
if (s.contains(num)) {
71+
res.add(num);
72+
}
73+
}
74+
int[] output = new int[res.size()];
6275
int i = 0;
63-
for (Integer e : s1) {
64-
output[i++] = e;
76+
for (int num : res) {
77+
output[i++] = num;
6578
}
6679
return output;
6780
}
81+
}
82+
```
6883

69-
private Set<Integer> transfer(int[] nums) {
70-
Set<Integer> s = new HashSet<>();
71-
for (int e : nums) {
72-
s.add(e);
84+
### **JavaScript**
85+
86+
```js
87+
/**
88+
* @param {number[]} nums1
89+
* @param {number[]} nums2
90+
* @return {number[]}
91+
*/
92+
var intersection = function(nums1, nums2) {
93+
const s = new Set();
94+
for (const num of nums1) {
95+
s.add(num);
96+
}
97+
let res = new Set();
98+
for (const num of nums2) {
99+
if (s.has(num)) {
100+
res.add(num);
73101
}
74-
return s;
75102
}
76-
}
103+
return [...res];
104+
};
77105
```
78106

79107
### **...**

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

+40-14
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,60 @@
4040
```python
4141
class Solution:
4242
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
43-
s1, s2 = set(nums1), set(nums2)
44-
return list(s1 & s2)
43+
s = set(nums1)
44+
res = set()
45+
for num in nums2:
46+
if num in s:
47+
res.add(num)
48+
return list(res)
4549
```
4650

4751
### **Java**
4852

4953
```java
5054
class Solution {
5155
public int[] intersection(int[] nums1, int[] nums2) {
52-
Set<Integer> s1 = transfer(nums1);
53-
Set<Integer> s2 = transfer(nums2);
54-
s1.retainAll(s2);
55-
int[] output = new int[s1.size()];
56+
Set<Integer> s = new HashSet<>();
57+
for (int num : nums1) {
58+
s.add(num);
59+
}
60+
Set<Integer> res = new HashSet<>();
61+
for (int num : nums2) {
62+
if (s.contains(num)) {
63+
res.add(num);
64+
}
65+
}
66+
int[] output = new int[res.size()];
5667
int i = 0;
57-
for (Integer e : s1) {
58-
output[i++] = e;
68+
for (int num : res) {
69+
output[i++] = num;
5970
}
6071
return output;
6172
}
73+
}
74+
```
6275

63-
private Set<Integer> transfer(int[] nums) {
64-
Set<Integer> s = new HashSet<>();
65-
for (int e : nums) {
66-
s.add(e);
76+
### **JavaScript**
77+
78+
```js
79+
/**
80+
* @param {number[]} nums1
81+
* @param {number[]} nums2
82+
* @return {number[]}
83+
*/
84+
var intersection = function(nums1, nums2) {
85+
const s = new Set();
86+
for (const num of nums1) {
87+
s.add(num);
88+
}
89+
let res = new Set();
90+
for (const num of nums2) {
91+
if (s.has(num)) {
92+
res.add(num);
6793
}
68-
return s;
6994
}
70-
}
95+
return [...res];
96+
};
7197
```
7298

7399
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
22
public int[] intersection(int[] nums1, int[] nums2) {
3-
Set<Integer> s1 = transfer(nums1);
4-
Set<Integer> s2 = transfer(nums2);
5-
s1.retainAll(s2);
6-
int[] output = new int[s1.size()];
3+
Set<Integer> s = new HashSet<>();
4+
for (int num : nums1) {
5+
s.add(num);
6+
}
7+
Set<Integer> res = new HashSet<>();
8+
for (int num : nums2) {
9+
if (s.contains(num)) {
10+
res.add(num);
11+
}
12+
}
13+
int[] output = new int[res.size()];
714
int i = 0;
8-
for (Integer e : s1) {
9-
output[i++] = e;
15+
for (int num : res) {
16+
output[i++] = num;
1017
}
1118
return output;
1219
}
13-
14-
private Set<Integer> transfer(int[] nums) {
15-
Set<Integer> s = new HashSet<>();
16-
for (int e : nums) {
17-
s.add(e);
18-
}
19-
return s;
20-
}
2120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
var intersection = function(nums1, nums2) {
7+
const s = new Set();
8+
for (const num of nums1) {
9+
s.add(num);
10+
}
11+
let res = new Set();
12+
for (const num of nums2) {
13+
if (s.has(num)) {
14+
res.add(num);
15+
}
16+
}
17+
return [...res];
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Solution:
22
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
3-
s1, s2 = set(nums1), set(nums2)
4-
return list(s1 & s2)
3+
s = set(nums1)
4+
res = set()
5+
for num in nums2:
6+
if num in s:
7+
res.add(num)
8+
return list(res)

solution/0300-0399/0350.Intersection of Two Arrays II/README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,77 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
“哈希表”实现。
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
<!-- 这里可写当前语言的特殊实现逻辑 -->
5153

5254
```python
53-
55+
class Solution:
56+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
57+
counter = collections.Counter(nums1)
58+
res = []
59+
for num in nums2:
60+
if counter[num] > 0:
61+
res.append(num)
62+
counter[num] -= 1
63+
return res
5464
```
5565

5666
### **Java**
5767

5868
<!-- 这里可写当前语言的特殊实现逻辑 -->
5969

6070
```java
71+
class Solution {
72+
public int[] intersect(int[] nums1, int[] nums2) {
73+
Map<Integer, Integer> counter = new HashMap<>();
74+
for (int num : nums1) {
75+
counter.put(num, counter.getOrDefault(num, 0) + 1);
76+
}
77+
List<Integer> intersection = new ArrayList<>();
78+
for (int num : nums2) {
79+
int val = counter.getOrDefault(num, 0);
80+
if (val > 0) {
81+
intersection.add(num);
82+
counter.put(num, val - 1);
83+
}
84+
}
85+
int i = 0;
86+
int[] res = new int[intersection.size()];
87+
for (int num : intersection) {
88+
res[i++] = num;
89+
}
90+
return res;
91+
}
92+
}
93+
```
6194

95+
### **JavaScript**
96+
97+
```js
98+
/**
99+
* @param {number[]} nums1
100+
* @param {number[]} nums2
101+
* @return {number[]}
102+
*/
103+
var intersect = function(nums1, nums2) {
104+
const counter = {};
105+
for (const num of nums1) {
106+
counter[num] = (counter[num] || 0) + 1;
107+
}
108+
let res = [];
109+
for (const num of nums2) {
110+
if (counter[num] > 0) {
111+
res.push(num);
112+
counter[num] -= 1;
113+
}
114+
}
115+
return res;
116+
};
62117
```
63118

64119
### **...**

solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,67 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
52+
counter = collections.Counter(nums1)
53+
res = []
54+
for num in nums2:
55+
if counter[num] > 0:
56+
res.append(num)
57+
counter[num] -= 1
58+
return res
5159
```
5260

5361
### **Java**
5462

5563
```java
56-
64+
class Solution {
65+
public int[] intersect(int[] nums1, int[] nums2) {
66+
Map<Integer, Integer> counter = new HashMap<>();
67+
for (int num : nums1) {
68+
counter.put(num, counter.getOrDefault(num, 0) + 1);
69+
}
70+
List<Integer> intersection = new ArrayList<>();
71+
for (int num : nums2) {
72+
int val = counter.getOrDefault(num, 0);
73+
if (val > 0) {
74+
intersection.add(num);
75+
counter.put(num, val - 1);
76+
}
77+
}
78+
int i = 0;
79+
int[] res = new int[intersection.size()];
80+
for (int num : intersection) {
81+
res[i++] = num;
82+
}
83+
return res;
84+
}
85+
}
5786
```
5887

88+
### **JavaScript**
89+
90+
```js
91+
/**
92+
* @param {number[]} nums1
93+
* @param {number[]} nums2
94+
* @return {number[]}
95+
*/
96+
var intersect = function(nums1, nums2) {
97+
const counter = {};
98+
for (const num of nums1) {
99+
counter[num] = (counter[num] || 0) + 1;
100+
}
101+
let res = [];
102+
for (const num of nums2) {
103+
if (counter[num] > 0) {
104+
res.push(num);
105+
counter[num] -= 1;
106+
}
107+
}
108+
return res;
109+
};
110+
59111
### **...**
60112

61113
```

0 commit comments

Comments
 (0)