Java 如何获得ArrayList的最小数量<;整数>;需要有从1到“的所有数字;n";?

Java 如何获得ArrayList的最小数量<;整数>;需要有从1到“的所有数字;n";?,java,arraylist,Java,Arraylist,我正在寻找一个简单的解决方案,以获得所有数字从1到n所需的最小数“ArrayList of Integers”,下一个条件是: 每个ArrayList必须由3个参数(a、b和n)创建 “a”和“b”设置ArrayList中的数字 “n”是极限 条件是: 如果≤ b=>a≤ J≤ b 如果a>b==>1≤ J≤ b和a≤ J≤ n 注:“j”是ArrayList上的数字 这是我的代码: public Integers(int a, int b, int n) {//constructor

我正在寻找一个简单的解决方案,以获得所有数字从1到n所需的最小数“ArrayList of Integers”,下一个条件是:

每个ArrayList必须由3个参数(a、b和n)创建

“a”和“b”设置ArrayList中的数字

“n”是极限

条件是:

如果≤ b=>a≤ J≤ b

如果a>b==>1≤ J≤ b和a≤ J≤ n

注:“j”是ArrayList上的数字

这是我的代码:

public Integers(int a, int b, int n) {//constructor
    this.numbers = new ArrayList<Integer>();

    makeList(a, b, n);
}

public void makeList(int a, int b, int n) {
    if (a < b) {
        while (a <= b) {
            this.numbers.add(a);
            a++;
        }
    } else {

        int aux = 1;
        while (aux <= b) {
            this.numbers.add(aux);
            aux++;
        }

        int aux2 = a;
        while (aux2 <= n) {
            this.numbers.add(aux2);
            aux2++;
        }

    }
}

public void showNumbers() {
    for (int x = 0; x < this.numbers.size(); x++) {
        System.out.print(this.numbers.get(x) + "\t");
    }

}
公共整数(inta,intb,intn){//constructor
this.numbers=新的ArrayList();
制造清单(a、b、n);
}
公共void生成列表(int a、int b、int n){
if(a而(a如果您从一开始就知道
n
是什么,那么存储
n
值的布尔数组可能更简单。然后每次构建
ArrayList
,您只需标记该值是否会出现在
ArrayList

除此之外,您几乎必须使用蛮力(我认为这相当于顶点覆盖问题,因此您最多能够以比蛮力更快的时间进行近似)

因此,我将尝试实现您的
Integer
类:

public class Integer {
    private int a, b;
    private boolean flipped;

    public Integer(int _a, int _b){
        a = Math.min(_a, _b);
        b = Math.max(_a, _b);
        flipped = b < a;
    }

    public void markOff(boolean [] arr){
        for(int i = 0; i < arr.length; i++){
            if(a <= i && i <= b){
                arr[i] = arr[i] || !flipped;
            }else{
                arr[i] = arr[i] || flipped;
            }
        }
    }
}

直观地说,我在这里做的是为每个输入的封面,选择是否使用它,并继续查看其余部分。这里的递归“记住”我的选择。我保证(遗憾地)这样做检查所有选项,这样做速度很慢,但绝对正确。一个优化可能是忽略子集:如果一个数组只包含1个数组中已经包含的项,则放弃它。

第二个用例的预期输出是什么?每个用例都可以根据您的情况进行调整。不确定您需要什么?
public class Integer {
    private int a, b;
    private boolean flipped;

    public Integer(int _a, int _b){
        a = Math.min(_a, _b);
        b = Math.max(_a, _b);
        flipped = b < a;
    }

    public void markOff(boolean [] arr){
        for(int i = 0; i < arr.length; i++){
            if(a <= i && i <= b){
                arr[i] = arr[i] || !flipped;
            }else{
                arr[i] = arr[i] || flipped;
            }
        }
    }
}
public static int driveSoln(int n, Integer[] covers){
    return helper(covers, new boolean[n], 0, 0);
}

private static int helper(Integer[] covers, boolean[] marked, int idx, int used){
    boolean done;
    for(boolean d: marked) done = done && d;
    if(done) return used;

    if(idx >= covers.length) return -1;

    boolean [] markCopy = marked.clone();
    covers[i].markOff(marked);
    int dontUse = helper(covers, markCopy, idx + 1, used);
    int use = helper(covers, marked, idx + 1, used + 1);
    return Math.min(use, dontUse);
}