mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
@ -121,7 +121,7 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
版本一:使用HashSet
|
||||
```Java
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@ -159,7 +159,28 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
版本二:使用Hash數組
|
||||
```java
|
||||
class Solution {
|
||||
public int[] intersection(int[] nums1, int[] nums2) {
|
||||
int[] hash1 = new int[1002];
|
||||
int[] hash2 = new int[1002];
|
||||
for(int i : nums1)
|
||||
hash1[i]++;
|
||||
for(int i : nums2)
|
||||
hash2[i]++;
|
||||
List<Integer> resList = new ArrayList<>();
|
||||
for(int i = 0; i < 1002; i++)
|
||||
if(hash1[i] > 0 && hash2[i] > 0)
|
||||
resList.add(i);
|
||||
int index = 0;
|
||||
int res[] = new int[resList.size()];
|
||||
for(int i : resList)
|
||||
res[index++] = i;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
```
|
||||
### Python3:
|
||||
(版本一) 使用字典和集合
|
||||
|
||||
|
Reference in New Issue
Block a user