[Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]

【问题描述】[中等]

[Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]_第1张图片

【解答思路】

1. 哈希映射

[Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]_第2张图片

复杂度分析
在这里插入图片描述

class Solution {
     
    public int[] intersect(int[] nums1, int[] nums2) {
     
        if (nums1.length > nums2.length) {
     
            return intersect(nums2, nums1);
        }
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int num : nums1) {
     
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        int[] intersection = new int[nums1.length];
        int index = 0;
        for (int num : nums2) {
     
            int count = map.getOrDefault(num, 0);
            if (count > 0) {
     
                intersection[index++] = num;
                count--;
                if (count > 0) {
     
                    map.put(num, count);
                } else {
     
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}
 public int[] intersect(int[] nums1, int[] nums2) {
     
        HashMap<Integer, Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for(int num : nums1) {
     
            if(!map.containsKey(num)) map.put(num, 1);
            else map.put(num, map.get(num) + 1);
        }
        for(int num : nums2) {
     
            if(map.containsKey(num)) {
     
                map.put(num, map.get(num) - 1);
                if(map.get(num) == 0) map.remove(num);
                list.add(num);
            }
        }
        int[] res = new int[list.size()];
        for(int i = 0; i < list.size(); i++) {
     
            res[i] = list.get(i);
        }
        return res;
    }




2. 双指针

[Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]_第3张图片
复杂度分析
[Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]_第4张图片

class Solution {
     
    public int[] intersect(int[] nums1, int[] nums2) {
     
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int length1 = nums1.length, length2 = nums2.length;
        int[] intersection = new int[Math.min(length1, length2)];
        int index1 = 0, index2 = 0, index = 0;
        while (index1 < length1 && index2 < length2) {
     
            if (nums1[index1] < nums2[index2]) {
     
                index1++;
            } else if (nums1[index1] > nums2[index2]) {
     
                index2++;
            } else {
     
                intersection[index] = nums1[index1];
                index1++;
                index2++;
                index++;
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}


  public int[] intersect(int[] nums1, int[] nums2) {
     
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        List<Integer> list = new ArrayList<>();
        int p1 = 0, p2 = 0;
        while(p1 < nums1.length && p2 < nums2.length) {
     
            if(nums1[p1] < nums2[p2]) p1++;
            else if(nums1[p1] > nums2[p2]) p2++;
            else {
     
                list.add(nums1[p1]);
                p1++;
                p2++;
            }
        }
        int[] res = new int[list.size()];
        for(int i = 0; i < res.length; i++) res[i] = list.get(i);
        return res;
    }
3. 辅助数组(不推荐)

时间复杂度:O(N^2) 空间复杂度:O(N)

    public int[] intersect(int[] nums1, int[] nums2) {
     
        int len1 = nums1.length;
        int len2 = nums2.length;
        
        List<Integer> ans = new ArrayList<>();
        if(len1<=len2){
     
            ans = cross(len1,len2,nums1,nums2);
        }
        else{
     
            //len1>len2
            ans = cross(len2,len1,nums2,nums1);
        }
        int[] res = new int[ans.size()];
        for(int i = 0; i < res.length; i++) 
            res[i] = ans.get(i);
        return res;
    }

    public List<Integer> cross(int shao,int duo,int[] nums1,int[] nums2 ){
     
        List<Integer> ans = new ArrayList<>();
    boolean[] a = new boolean[duo]; 
    for(int num:nums1){
     
        for(int i = 0;i<duo;i++){
     
            if(num == nums2[i] && !a[i]){
     
                ans.add(num);
                a[i]=true;
                break;
            }
        }
    }
    return ans;
    }

【总结】

1.函数返回一个长度不确定的数组 (int[ ])

方法一:List转Int数组 (逐个复制)

List<Integer> list = new ArrayList<Integer>();
//LinkedList list = new LinkedList();
 list.add(1);
 list.add(2);
 int count = list.size();
        int[] aux = new int[count];
        for(int i = 0; i < count; i++){
     
            aux[i] = list.poll();
        }
        return aux;

方法二:使用函数Arrays.copyOfRange

int length1 = nums1.length, length2 = nums2.length;
int[] intersection = new int[Math.min(length1, length2)];
return Arrays.copyOfRange(intersection, 0, index);
2.交集问题 双指针 HashMap
3.String[] 与 List < String > 互转
		String[] strings1 = {
     "a", "b", "c"};
        // String[] 转 List
        List<String> list3 = Arrays.asList(strings1);
        // List 转 String[]
        String[] strings2 = list3.toArray(new String[0]);

参考链接:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/solution/liang-ge-shu-zu-de-jiao-ji-ii-by-leetcode-solution/

参考链接:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/solution/liang-chong-jie-fa-javashi-xian-by-lyl0724-2/

你可能感兴趣的:(非0即1,刷题,leetcode,hashmap,字符串,java,双指针)