2021-08-24 集合中查找子集

package net.sahv.sssmiao.controller.wap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

import cn.hutool.core.convert.Convert;

public class SubListExport2 {
	public static String[] ticket1 = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
			"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
			"32", "33", "34", "35" };

	public static String[] ticket2 = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", };

	public static void main(String[] args) {
		List<String> strings = Arrays.asList(ticket1);
		List<String> strings2 = Arrays.asList(ticket2);
		if (strings.size() == 2) {

		} else {
			List<List<String>> re1 = new ArrayList<>();
			macSortStoredValue(re1, new Stack<>(), strings, 5, 0, 0);
//			for (List<String> ss : re1) {
//				System.out.println(ss);
//			}
			List<List<String>> re2 = new ArrayList<>();
			macSortStoredValue(re2, new Stack<>(), strings2, 2, 0, 0);
//			for (List<String> ss : re2) {
//				System.out.println(ss);
//			}
			System.out.println(re1.size());
			System.out.println(re2.size());
			int i=0;
			aa:for (List<String> ss : re1) {
				for (List<String> ss2 : re2) {
					System.out.println(Convert.toStr(ss).replace("[", "").replace("]", ""));
					//System.out.println(ss+""+ss2);
					i++;
//					if(i>200) {
//						break aa;
//					}
//					if(i>100 &&i<=200) {
//						System.out.println(ss+""+ss2);
//					}
					
				}
			}
			System.out.println(i);
		}
	}
	
	

	/**
	 * 
	 * @param stringList 返回的集合
	 * @param stack 临时
	 * @param shu 子集长度
	 * @param targ  源
	 * @param has 当前长度
	 * @param cur 指针
	 */
	public static void macSortStoredValue(List<List<String>> stringList, Stack<String> stack, List<String> shu,
			int targ, int has, int cur) {
//		System.out.println(stack);
		if (has == targ) {
			if(iscontinuity(stack,3,ticket1)) {
//				System.out.println(stack);
				return;
			}
			List<String> s = new ArrayList<String>(stack);
			stringList.add(s);
			return;
		}
		for (int i = cur; i < shu.size(); i++) {
			if (!stack.contains(shu.get(i))) {
				stack.add(shu.get(i));
				macSortStoredValue(stringList, stack, shu, targ, has + 1, i);
				stack.pop();
			}
		}
	}

	/**
	 * 判断是否连续
	 
	 */
	private static boolean iscontinuity(Stack<String> stack,Integer length,String[] tick) {
		 
		if(stack.size()<length) {
			return false;
		}else {
			String first = stack.get(0);
			int i = 0;
			for( int y = 0;i<tick.length; y++) {
				if(tick[y].equals(first)) {
					i=y;
					break;
				}
			}
			for(int z=1;z<length;z++) {
				i++;
				if(!stack.get(z).equals(tick[i])) {
					return false;
				}
			}
			
		}
		return true;
		
	}
	
	public static void macSortStoredValuetwo(List<List<String>> stringList, Stack<String> stack, List<String> shu,
			int targ, int has, int cur) {
//		System.out.println(stack);
		if (has == targ) {
			List<String> s = new ArrayList<String>(stack);
			stringList.add(s);
			return;
		}
		for (int i = cur; i < shu.size(); i++) {
			if (!stack.contains(shu.get(i))) {
				stack.add(shu.get(i));
				macSortStoredValuetwo(stringList, stack, shu, targ, has + 1, i);
				stack.pop();
			}
		}
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值