Java learning manuals :( data structures and algorithms - array) how to calculate the intersection of two ordered array of integers?

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/MaybeForever/article/details/102417313

Problem Description:

Suppose two elements containing n ordered (non-descending) array of integers a and b, where a = {0,1,2,3,4}, b = {1,3,5,7,9}, then their intersection is {1,3}.

method one:

Merging law. Two arrays are disposed array1 [n1], array2 [n2], the i, j traversing two arrays respectively, during the traversal, if the current traverse position array1 [i] and array2 [j] equal, this number is the intersection of two arrays, recorded and continue to traverse back and array1 array. If array1 [i] is greater than array2 [j], we shall continue traversing rearwardly array2. If array1 [i] is less than array2 [j], shall continue to be backwards traversal array1. Until the end of an array traversal ends.

The method of one code is as follows:

package com.haobi;

import java.util.ArrayList;

public class Test27 {
	public static void main(String[] args) {
		int[] a = {0,1,2,3,4};
		int[] b = {1,3,5,7,9};
		ArrayList<Integer> mix = mixed(a, b);
		for(int i=0;i<mix.size();i++) {
			System.out.print(mix.get(i)+" ");
		}
	}
	
	public static ArrayList<Integer> mixed(int array1[], int array2[]){
		ArrayList<Integer> mix = new ArrayList<Integer>();
		int i=0,j=0;
		int n1 = array1.length;
		int n2 = array2.length;
		while(i<n1 && j<n2) {
			if(array1[i]==array2[j]) {
				mix.add(array1[i]);
				i++;
				j++;
			}else if(array1[i]>array2[j]) {
				j++;
			}else if(array1[i]<array2[j]) {
				i++;
			}
		}
		return mix;
	}
}

Program output results are as follows:

1 3 

Method Two:

Order traversal method. Sequentially traversing the two arrays, the array elements stored in the hash table, while the count array elements, if it is 2, for the intersection of both elements.

Method two code is as follows:

package com.haobi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Test28 {
	public static void main(String[] args) {
		int[] a = {0,1,2,3,4};
		int[] b = {1,3,5,7,9};
		ArrayList<Integer> mix = mixed(a, b);
		for(int i=0;i<mix.size();i++) {
			System.out.print(mix.get(i)+" ");
		}
	}
	
	public static ArrayList<Integer> mixed(int array1[], int array2[]){
		ArrayList<Integer> mix = new ArrayList<Integer>();
		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
		int n1 = array1.length;
		int n2 = array2.length;
		//遍历array1
		for(int i=0;i<n1;i++) {
			if(map.containsKey(array1[i])) {
				map.put(array1[i], map.get(array1[i])+1);
			}else {
				map.put(array1[i], 1);					
			}
		}
		//遍历array2
		for(int j=0;j<n2;j++) {
			if(map.containsKey(array2[j])) {
				map.put(array2[j], map.get(array2[j])+1);
			}else {
				map.put(array2[j], 1);					
			}
		}
		//遍历Map
		for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
			if(entry.getValue() == 2) {
				mix.add(entry.getKey());
			}
		}
		return mix;
	}
}

Program output results are as follows:

1 3 

Guess you like

Origin blog.csdn.net/MaybeForever/article/details/102417313