import java.util.*;
public class Solution {
public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
// write code here
HashSet<Integer> hash = new HashSet<Integer>();
ArrayList<Integer> arr = new ArrayList<Integer>();
for(Integer x : nums1){
hash.add(x);
}
for(Integer x : nums2){
if(hash.contains(x)&& !arr.contains(x)){
arr.add(x);
}
}
return arr;
}
}
//
mport java.util.*;
public class Solution
{
public ArrayList<Integer> intersection (ArrayList<Integer> nums1,
ArrayList<Integer> nums2)
{
boolean[] hash = new boolean[1010];
for(int x : nums1)
{
hash[x] = true;
}
ArrayList<Integer> ret = new ArrayList<>();
for(int x : nums2)
{
if(hash[x])
{
ret.add(x);
hash[x] = false;
}
}
return ret;
}
标签:arr,hash,交集,ArrayList,数组,new,两个,nums1,nums2 From: https://blog.csdn.net/qq_61576108/article/details/140336873