有两个有序的集合,集合的每个元素都是一段范围,求其交集,

package com.tulun;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**  
* 描述:有两个有序的集合,集合的每个元素都是一段范围,求其交集,
* 例如交集{[4,8],[9,13]}和{[6,12]}的交集是{[6,8],[9,12]} 
*
 * 两个有序的集合,集合的每个元素都是一段范围,求其交集
 * 所求交集结果直接打印到输出终端
 * @param collects1:集合1
 * @param collects2:集合2
 *

*  
* @author ASUS  
* @date 2018年8月4日
*/
public class TestGY1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//测试题中给的数据是否正确
		List <List <Integer>> collection1 = new ArrayList <List <Integer>>();
        ArrayList <Integer> list11 = new ArrayList <>();
        list11.add(4);
        list11.add(8);
        collection1.add(list11);

        ArrayList <Integer> list12 = new ArrayList <>();
        list12.add(9);
        list12.add(13);
        collection1.add(list12);

        ArrayList <List <Integer>> collection2 = new ArrayList <>();
        ArrayList <Integer> list21 = new ArrayList <>();
        list21.add(6);
        list21.add(12);
        collection2.add(list21);
        
        intersectionWithColletion(collection1,collection2);
	}
	
	//最终的交集
	public static void intersectionWithColletion(List<List<Integer>> collects1, List<List<Integer>> collects2) {
        //参数校验
        if (!(isVaild(collects1) && isVaild(collects2))) {
            return;
        }
          //把每个集合中的小集合都与其他的做交集判断
        Iterator <List <Integer>> iterator1 = collects1.iterator();
        while (iterator1.hasNext()) {
            List <Integer> list1 = iterator1.next();
            Iterator <List <Integer>> iterator2 = collects2.iterator();
            while (iterator2.hasNext()) {
                List <Integer> list2 = iterator2.next();
                intersection(list1, list2);
            }
        }
	}
	
	//交集的算法过程
        private static void intersection(List<Integer> list1, List<Integer> list2) {
        	
            Integer a1 = list1.get(0);
            Integer a2 = list1.get(1);
            Integer b1 = list2.get(0);
            Integer b2 = list2.get(1);

            if (a1 > b2 || b1 > a2) {
                return;
            }
            
            if (a1 == b2) {
                System.out.println("["+a1+"]");
            }
            
            if (a2 == b1) {
                System.out.println("["+a2+"]");
            }
            
            if (a1 > b1) {
                System.out.print("[" + a1 + ",");
                if (b2 < a2) {
                    System.out.println(b2 + "]");
                } else {
                    System.out.println(a2 + "]");
                }
            }
            
            if (b1 >=  a1) {
                System.out.print("["+b1+",");
                if (b2 < a2) {
                    System.out.println(b2 + "]");
                } else {
                    System.out.println(a2 + "]");
                }
            }
            
        }
        /*
         * 参数校验的方法
         */
        private static boolean isVaild(List<List<Integer>> collects) {
            if (collects.size() < 1) {
                System.out.println("集合为空");
                return false;
            }
            Iterator <List <Integer>> iterator = collects.iterator();
            while (iterator.hasNext()) {
                List <Integer> list = iterator.next();
                if (list.size() != 2) {
                    System.out.println("集合区间不合法");
                    return false;
                }
                if (list.get(0) > list.get(1)) {
                    System.out.println("集合区间不是有序的");
                    return false;
                }
            }
                return true;
        }



    


}

猜你喜欢

转载自blog.csdn.net/qq_41974391/article/details/81409197