Merge pull request #2234 from Lozakaka/master-1

新增java解法(hashArray)
This commit is contained in:
程序员Carl
2023-08-28 09:33:04 +08:00
committed by GitHub

View File

@ -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
(版本一) 使用字典和集合