Skip to content

Commit 0e05469

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0349
1 parent 430b8ae commit 0e05469

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,38 @@
3636
<!-- 这里可写当前语言的特殊实现逻辑 -->
3737

3838
```python
39-
39+
class Solution:
40+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
41+
s1, s2 = set(nums1), set(nums2)
42+
return list(s1 & s2)
4043
```
4144

4245
### **Java**
4346

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

4649
```java
47-
50+
class Solution {
51+
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+
int i = 0;
57+
for (Integer e : s1) {
58+
output[i++] = e;
59+
}
60+
return output;
61+
}
62+
63+
private Set<Integer> transfer(int[] nums) {
64+
Set<Integer> s = new HashSet<>();
65+
for (int e : nums) {
66+
s.add(e);
67+
}
68+
return s;
69+
}
70+
}
4871
```
4972

5073
### **...**

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,36 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
49+
s1, s2 = set(nums1), set(nums2)
50+
return list(s1 & s2)
4851
```
4952

5053
### **Java**
5154

5255
```java
53-
56+
class Solution {
57+
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()];
62+
int i = 0;
63+
for (Integer e : s1) {
64+
output[i++] = e;
65+
}
66+
return output;
67+
}
68+
69+
private Set<Integer> transfer(int[] nums) {
70+
Set<Integer> s = new HashSet<>();
71+
for (int e : nums) {
72+
s.add(e);
73+
}
74+
return s;
75+
}
76+
}
5477
```
5578

5679
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
class Solution {
22
public int[] intersection(int[] nums1, int[] nums2) {
3-
HashSet<Integer> set1 = new HashSet<Integer>();
4-
for (Integer n : nums1) set1.add(n);
5-
HashSet<Integer> set2 = new HashSet<Integer>();
6-
for (Integer n : nums2) set2.add(n);
7-
8-
set1.retainAll(set2);
9-
10-
int [] output = new int[set1.size()];
11-
int idx = 0;
12-
for (int s : set1) output[idx++] = s;
3+
Set<Integer> s1 = transfer(nums1);
4+
Set<Integer> s2 = transfer(nums2);
5+
s1.retainAll(s2);
6+
int[] output = new int[s1.size()];
7+
int i = 0;
8+
for (Integer e : s1) {
9+
output[i++] = e;
10+
}
1311
return output;
1412
}
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+
}
1521
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
2-
def intersection(self, nums1: 'List[int]', nums2: 'List[int]') -> 'List[int]':
3-
4-
return list(set(nums1).intersection(set(nums2)))
2+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
s1, s2 = set(nums1), set(nums2)
4+
return list(s1 & s2)

0 commit comments

Comments
 (0)