算法题 - 求数组的子集合

求数组的子集合


记录面试算法题,便于回顾


题目一:

给出一个数组 list = [1,2,3,4,5,6]

求此数组的所有子集合

输出:[1],[1,2],[1,2,3]…


题目二:

给出一个数组 list = [1,2,3,4,5,6]

求此数组的所有子集合中和为6的子集

输出:[6,[2,4],[1,5],[1,2,3]


解题思路:

方式一:递归

方式二:全排列方式


方式一:递归


算法
package com.tianya.java.algorithm;

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

/**
 * @description
 *	求数组的子集合,子数组
 *	方式一:递归方式
 * @author TianwYam
 * @date 2021年6月1日下午2:37:21
 */
public class SubArray {
	
	public static void main(String[] args) {
		
		// 测试数组
		int[] list = {1,2,3,4,5,6} ;
		
		// 保存结果集
		List<List<Integer>> resultList = new ArrayList<>();
		List<Integer> subList = new ArrayList<>();
		
		// 递归调用
		subList(list, 0, resultList, subList);
		
		// 求数组的子集
		System.out.println("数组的所有子集合:");
		for (List<Integer> sub : resultList) {
			System.out.println(Arrays.toString(sub.toArray()));
		}
		
		// 求各个子集中 和 为6的集合
		System.out.println("输出和为:6");
		for (List<Integer> sub : resultList) {
			int sum = sub.stream().reduce(0, Integer::sum);
			if (sum == 6) {
				System.out.println(Arrays.toString(sub.toArray()));
			}
		}
	}
	
	
	/**
	 * @description
	 *	递归 
	 * @author TianwYam
	 * @date 2021年6月1日下午3:22:41
	 * @param list
	 * @param index
	 * @param resultList
	 * @param subList
	 */
	public static void subList(int[] list , int index, 
			List<List<Integer>> resultList, List<Integer> subList) {
		
		resultList.add(new ArrayList<>(subList));
		
		int size = list.length ;
		for (int i = index; i < size; ) {
			subList.add(list[i]);
			subList(list, ++i, resultList, subList);
			subList.remove(subList.size() - 1);
		}
		
	}
	

}

结果
数组的所有子集合:
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 5]
[1, 2, 3, 5, 6]
[1, 2, 3, 6]
[1, 2, 4]
[1, 2, 4, 5]
[1, 2, 4, 5, 6]
[1, 2, 4, 6]
[1, 2, 5]
[1, 2, 5, 6]
[1, 2, 6]
[1, 3]
[1, 3, 4]
[1, 3, 4, 5]
[1, 3, 4, 5, 6]
[1, 3, 4, 6]
[1, 3, 5]
[1, 3, 5, 6]
[1, 3, 6]
[1, 4]
[1, 4, 5]
[1, 4, 5, 6]
[1, 4, 6]
[1, 5]
[1, 5, 6]
[1, 6]
[2]
[2, 3]
[2, 3, 4]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[2, 3, 4, 6]
[2, 3, 5]
[2, 3, 5, 6]
[2, 3, 6]
[2, 4]
[2, 4, 5]
[2, 4, 5, 6]
[2, 4, 6]
[2, 5]
[2, 5, 6]
[2, 6]
[3]
[3, 4]
[3, 4, 5]
[3, 4, 5, 6]
[3, 4, 6]
[3, 5]
[3, 5, 6]
[3, 6]
[4]
[4, 5]
[4, 5, 6]
[4, 6]
[5]
[5, 6]
[6]
输出和为:6
[1, 2, 3]
[1, 5]
[2, 4]
[6]

方式二:全排列组合


算法
package com.tianya.java.algorithm;

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

/**
 * @description
 *	求数组的子集合,子数组
 *	方式二:全排列方式
 * @author TianwYam
 * @date 2021年6月1日下午2:37:21
 */
public class SubArray2 {
	
	public static void main(String[] args) {
		
		// 数组
		int[] list = {1,2,3,4,5,6} ;
		
		// 保存结果集 
		List<List<Integer>> resultList = new ArrayList<>();
		
		// 全排列 循环
		int size = list.length ;
		for (int i = 0; i < size; i++) {
			
			// 保存单个
			resultList.add(new ArrayList<>(Arrays.asList(list[i])));
			
			for (int j = i+1; j < size; j++) {
				
				List<Integer> subList = new ArrayList<>();
				subList.add(list[i]);
				
				for (int k = j; k < size; k++) {
					subList.add(list[k]);
					
					// 保存重新一个数组
					resultList.add(new ArrayList<>(subList));
				}
			}
			
		}
		
		
		
		System.out.println("数组的所有子集合:");
		for (List<Integer> sub : resultList) {
			System.out.println(Arrays.toString(sub.toArray()));
		}
		
		
		System.out.println("输出和为:6");
		for (List<Integer> sub : resultList) {
			int sum = sub.stream().reduce(0, Integer::sum);
			if (sum == 6) {
				System.out.println(Arrays.toString(sub.toArray()));
			}
		}
	}
	
	

}


结果
数组的所有子集合:
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 3]
[1, 3, 4]
[1, 3, 4, 5]
[1, 3, 4, 5, 6]
[1, 4]
[1, 4, 5]
[1, 4, 5, 6]
[1, 5]
[1, 5, 6]
[1, 6]
[2]
[2, 3]
[2, 3, 4]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[2, 4]
[2, 4, 5]
[2, 4, 5, 6]
[2, 5]
[2, 5, 6]
[2, 6]
[3]
[3, 4]
[3, 4, 5]
[3, 4, 5, 6]
[3, 5]
[3, 5, 6]
[3, 6]
[4]
[4, 5]
[4, 5, 6]
[4, 6]
[5]
[5, 6]
[6]
输出和为:6
[1, 2, 3]
[1, 5]
[2, 4]
[6]



欢迎关注,谢谢!


刚开始写微信公众号,请多多关注,欢迎,多谢!

微信公众号:《Java学习积累》
请关注一下,多谢!!!
微信公众号:Java学习积累

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯共明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值