LeetBook--哈希表--两个数组的交集
This commit is contained in:
parent
55391ce725
commit
bbb80d3a6d
25
src/main/java/leet/book/hashTablePlus/Intersection.java
Normal file
25
src/main/java/leet/book/hashTablePlus/Intersection.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package leet.book.hashTablePlus;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Intersection {
|
||||||
|
class Solution {
|
||||||
|
public int[] intersection(int[] nums1, int[] nums2) {
|
||||||
|
Set<Integer> set = new HashSet<>();
|
||||||
|
for (int num : nums1) {
|
||||||
|
set.add(num);
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
for (int num : nums2) {
|
||||||
|
if (set.contains(num)) {
|
||||||
|
set.remove(num);
|
||||||
|
nums1[index] = num;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Arrays.copyOf(nums1, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user