• 集合的子集


    题目描述
    请编写一个方法,返回某集合的所有非空子集。

    给定一个int数组A和数组的大小int n,请返回A的所有非空子集。保证A的元素个数小于等于20,且元素互异。各子集内部从大到小排序,子集之间字典逆序排序,见样例。

    测试样例:
    [123,456,789]
    返回:{[789,456,123],[789,456],[789,123],[789],[456 123],[456],[123]}


    import java.util.*;

    public class Subset {
    ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> getSubsets(int[] A, int n) {
    // write code here
    getAllList(A, n-1, new ArrayList<Integer>());
    lists.remove(lists.size()-1);
    return lists;
    }

    private void getAllList(int[] A, int index, ArrayList<Integer> list) {
    if (index < 0) {
    lists.add(new ArrayList<Integer>(list));
    return;
    }
    list.add(A[index]);
    getAllList(A, index-1, list);
    list.remove(Integer.valueOf(A[index]));
    getAllList(A, index-1, list);
    }
    }

  • 相关阅读:
    Android天线信号刷新流程
    source insight配置
    android light sensor port
    wifi workflow
    vi/vim常用配置及使用命令
    contacts里QuickContactBadge弹出窗口
    ubuntu12.04与windows系统共享
    shell脚本编程
    凤凰照片的大小修改
    wf2
  • 原文地址:https://www.cnblogs.com/hyhy904/p/10958523.html
Copyright © 2020-2023  润新知