三種方法求解兩個陣列的交集
阿新 • • 發佈:2019-02-04
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); } }