简体   繁体   English

查找给定大小为n的大小为k的子集

[英]Finding subsets of size k for a given set of size n

I am trying to work out the solution to the above problem and I came up with this 我正在尝试找出上述问题的解决方案,我想出了这个

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class Subset_K {
public static void main(String[]args)
{
Set<String> x;
int n=4;
int k=2;
int arr[]={1,2,3,4};
StringBuilder sb=new StringBuilder();
for(int i=1;i<=(n-k);i++)
    sb.append("0");
for(int i=1;i<=k;i++)
    sb.append("1");
String bin=sb.toString();
x=generatePerm(bin);
Set<ArrayList <Integer>> outer=new HashSet<ArrayList <Integer>>();
for(String s:x){
    int dec=Integer.parseInt(s,2);
    ArrayList<Integer> inner=new ArrayList<Integer>();
    for(int j=0;j<n;j++){
        if((dec&(1<<j))>0)
            inner.add(arr[j]);
    }
    outer.add(inner);
}
for(ArrayList<Integer> z:outer){
    System.out.println(z);
}
}

public static Set<String> generatePerm(String input)
{
Set<String> set = new HashSet<String>();
if (input == "")
    return set;

Character a = input.charAt(0);

if (input.length() > 1)
{
    input = input.substring(1);

    Set<String> permSet = generatePerm(input);

    for (String x : permSet)
    {
        for (int i = 0; i <= x.length(); i++)
        {
            set.add(x.substring(0, i) + a + x.substring(i));
        }
    }
}
else
{
    set.add(a + "");
}
return set;
}
}

I am working on a 4 element set for test purpose and using k=2. 我正在研究一个4元素集用于测试目的并使用k = 2。 What I try to do is initially generate a binary string where k bits are set and nk bits are not set. 我尝试做的是最初生成一个二进制字符串,其中设置k位并且不设置nk位。 Now using this string I find all the possible permutations of this string. 现在使用这个字符串,我找到了这个字符串的所有可能的排列。 And then using these permutations I output the respective element in the set. 然后使用这些排列,我输出集合中的相应元素。 Now i cant figure out the complexity of this code because I used the generatePerm method from someone else. 现在我无法弄清楚这段代码的复杂性,因为我使用了其他人的generatePerm方法。 Can someone help me with the time complexity of the generatePerm method and also the overall time complexity of my solution. 有人可以帮助我解决generatePerm方法的时间复杂性以及我的解决方案的总体时间复杂性。 I found other recursive implementation of this problem in here Find all subsets of length k in an array However I cant figure out the complexity of it either. 我在这里找到了这个问题的其他递归实现在数组中查找长度为k的所有子集然而我也无法弄清楚它的复杂性。 So need some help there. 所以需要一些帮助。

Also I was trying to re-factor my code so that its not just for integers but for all types of data. 此外,我试图重新考虑我的代码,使其不仅适用于整数,而且适用于所有类型的数据。 I dont have much experience with generics. 我对泛型没什么经验。 so when I try to modify ArrayList< Integer> to ArrayList< ?> in line 21 eclipse says 所以当我尝试在第21行修改ArrayList <Integer>到ArrayList <?>时,eclipse说

Cannot instantiate the type ArrayList< ?> How do I correct that? 无法实例化类型ArrayList <?>如何更正?

You can use ArrayList<Object> throughout. 您可以在整个过程中使用ArrayList<Object> That will accept any kind of object. 这将接受任何类型的对象。 If you want a specific type that is determined by the calling code, you will need to introduce a generic type parameter. 如果需要由调用代码确定的特定类型,则需要引入泛型类型参数。

Note that in your generatePerm method, you should not use the test 请注意,在generatePerm方法中,不应使用该测试

if (input == "")

Instead, you should use: 相反,你应该使用:

if ("".equals(input))

Your current code will only succeed if input is the interned string "" . 只有当input实习字符串""您的当前代码才会成功。 It will not work, for instance, if input is computed as a substring() with zero length. 例如,如果将input计算为长度为零的substring() ,则它将不起作用。 In general you should always compare strings with .equals() rather than with == (except under very specific conditions when you are looking for object identity rather than object equality). 通常,您应该始终使用.equals()而不是使用==来比较字符串(在非常特定的条件下,当您查找对象标识而不是对象相等时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM