zoukankan      html  css  js  c++  java
  • 三种方法求解两个数组的交集

    package com.Test;
     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    //求两个数组的交集
    public class FindIntersectionBetweenTwoArrays {
        
        //算法一:暴力搜索,时间复杂度O(n^2),空间复杂度O(1)
        public ArrayList<Integer> getIntersection(int[] arr, int[] arr2) {
            
            ArrayList<Integer> res = new ArrayList<Integer>();
            if(arr == null || arr.length == 0 || arr2 == null || arr2.length == 0) {
                return res;
            }
            
            for(int i = 0;i < arr.length;i ++) {
                int temp = arr[i];
                for(int j = 0;j < arr.length;j ++) {
                    if(arr2[j] == temp && !res.contains(temp)) {
                        res.add(temp);
                        break;
                    }
                }
            }
            return res;
        }
        
        //算法二:先排序,然后定义两个指针,时间复杂度O(nlogn) (排序),空间复杂度O(1)
        public ArrayList<Integer> getIntersection2(int[] arr, int[] arr2) {
            
            ArrayList<Integer> res = new ArrayList<Integer>();
            if(arr == null || arr.length == 0 || arr2 == null || arr2.length == 0) {
                return res;
            }
            
            Arrays.sort(arr);
            Arrays.sort(arr2);
            int i = 0;
            int j = 0;
            while(i < arr.length && j < arr2.length) {
                if(arr[i] < arr2[j]) {
                    i ++;
                }
                else if(arr[i] > arr2[j]) {
                    j ++;
                }
                else {
                    if(!res.contains(arr[i])) {
                        res.add(arr[i]);
                    }
                    i ++;
                    j ++;
                }
            }
            return res;
        }
        
        //算法三:map计数,时间复杂度O(n),空间复杂度O(n),空间换时间
        public ArrayList<Integer> getIntersection3(int[] arr, int[] arr2) {
            
            ArrayList<Integer> res = new ArrayList<Integer>();
            if(arr == null || arr.length == 0 || arr2 == null || arr2.length == 0) {
                return res;
            }
            
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for(int i = 0;i < arr.length;i ++) {
                int key = arr[i];
                if(map.containsKey(key)) {
                    int value = map.get(key);
                    value ++;
                    map.put(key, value);
                }
                else {
                    map.put(key, 1);
                }
            }
            for(int i = 0;i < arr2.length;i ++) {
                int key = arr2[i];
                if(map.containsKey(key) && !res.contains(key)) {
                    res.add(key);
                }
            }
            return res;
            
        }
        
        public static void main(String[] args) {
            
            int[] arr = {1,2,5,3,3,4};
            int[] arr2 = {2,3,3,4,5,6};
            FindIntersectionBetweenTwoArrays fb = new FindIntersectionBetweenTwoArrays();
            List<Integer> res = fb.getIntersection3(arr, arr2);
            System.out.println(res);
        }
    }
    ————————————————
    版权声明:本文为CSDN博主「奇零可草」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/zhou15755387780/article/details/81317561
  • 相关阅读:
    基于RSA securID的Radius二次验证java实现(PAP验证方式)
    一些指令 & 一些知识 (Linux Spring log4j...)
    RSA, ACS5.X 集成配置
    Python中的动态属性与描述符
    设计模式(一):单例模式
    JavaWeb
    动态规划_背包问题
    动态规划_最长上升子序列
    MySQL复习
    动态规划_数字三角形
  • 原文地址:https://www.cnblogs.com/u013533289/p/11676719.html
Copyright © 2011-2022 走看看