在数字

时间:2016-04-21 16:07:03

标签: java performance for-loop arraylist counter

我正在尝试优化我的程序计数器。这取决于数字的大小(从3到10位,没有重复) - 例如。 012,013,214等。我的第一个解决方案是 for loops ,类似这样:

private void sample() {
        int[] varValue = new int[3];
        innerloop: for (int a = 0; a < 10; a++) {
            for (int b = 0; b < 10; b++) {
                if (a == b)
                    continue;
                for (int c = 0; c < 10; c++) {
                    if (c == b)
                        continue;
                    if (c == a)
                        continue;
                    varValue[0] = a;
                    varValue[1] = b;
                    varValue[2] = c;

                    int EqOne = varValue[0] * 100 + varValue[1] * 10 + varValue[2];

                    if (EqOne == 432) {
                        System.out.println(varValue[0]);
                        System.out.println(varValue[1]);
                        System.out.println(varValue[2]);
                        break innerloop;
                    }

                }
            }

        }
    }

或10位数(https://gist.github.com/TrollerN/6a0e470c539c57fd4cd73086cf6eb41b

然后我可以将那些 a,b,c 添加到int []或ArrayList中并使用它。没关系,但有10个不同的数字,10 for循环如果法则+我必须创建8种不同的方法 - 每个数字一个。

我还试图创建10位数(0,1,2,3,4,5,6,7,8,9)的ArrayList,将其洗牌,然后返回3-10位数,但它是&#39; s可以为9-10位数字制作永无止境的循环。

我的下一个&#34;伟大&#34;想法(不,老实说,到目前为止,一个人是最愚蠢的:D)是将组合的ArrayList保存到文件中,对于每种情况,然后加载所需的一个并通过它来寻找解决方案。

我正在考虑创建一些方法,它将采用数字位数(&#34;因此它知道它需要多少循环&#34;)和/或最后一个解决方案,因此它知道要开始。

编辑: 我用样本方法修改了问题。当然,int EqOne和If语句要复杂得多,但它技术上显示了我想要实现的目标 - 至少我希望:P

我正在寻找的是一种方法,可以根据需要创建尽可能多的循环和大数组/ arraylist?

2 个答案:

答案 0 :(得分:2)

假设您想要给定长度的数字,而不是字符串,一种方法是使用boolean[10]记住当前正在使用的数字,因此可以快速跳过它们

然后将数字保存在数组中,并像正常数字一样递增最后一位数字。当它翻转时,你递增倒数第二个数字,依此类推,将尾随数字重置为未使用的数字。

示例:如果当前号码为1097,则如下所示:

Digits  In Use               Description
1097    0 1 _ _ _ _ _ 7 _ 9
1097    0 1 _ _ _ _ _ _ _ 9  Clear in-use of last digit
1098    0 1 _ _ _ _ _ _ _ 9  Increment last digit
1098    0 1 _ _ _ _ _ _ 8 9  Mark in-use
=======================================================
1098    0 1 _ _ _ _ _ _ _ 9  Clear in-use of last digit
1099    0 1 _ _ _ _ _ _ _ 9  Increment last digit, but it's in use
109?    0 1 _ _ _ _ _ _ _ 9  Rollover, so go to previous digit
109?    0 1 _ _ _ _ _ _ _ _  Clear in-use
10??    0 1 _ _ _ _ _ _ _ _  Rollover, so go to previous digit
10??    _ 1 _ _ _ _ _ _ _ _  Clear in-use
11??    _ 1 _ _ _ _ _ _ _ _  Increment digit, but it's in use
12??    _ 1 _ _ _ _ _ _ _ _  Increment digit
12??    _ 1 2 _ _ _ _ _ _ _  Mark in-use
120?    0 1 2 _ _ _ _ _ _ _  Set to first unused digit, and mark in-use
1203    0 1 2 3 _ _ _ _ _ _  Set to first unused digit, and mark in-use
=======================================================

正如您所看到的,逻辑使其从1097变为10981203

这是逻辑。有关正在运行的示例,请参阅IDEONE

final class UniqueDigitCounter {
    private int[]     digits;
    private boolean[] inUse = new boolean[10];
    public UniqueDigitCounter(int digitCount) {
        if (digitCount < 1 || digitCount > 10)
            throw new IllegalArgumentException("Invalid digit count: " + digitCount);
        this.digits = new int[digitCount];
        for (int i = 0; i < digitCount; i++) {
            this.digits[i] = i;
            this.inUse[i] = true;
        }
    }
    public long next() {
        if (this.digits == null)
            return -1; // end of sequence
        long value = 0;
        for (int i = 0; i < this.digits.length; i++)
            value = value * 10 + this.digits[i];
        for (int i = this.digits.length - 1; ; i--) {
            if (i == -1) {
                this.digits = null; // end of sequence
                break;
            }
            int digit = this.digits[i];
            this.inUse[digit] = false;
            if ((digit = nextDigit(digit + 1)) != -1) {
                this.digits[i] = digit;
                while (++i < this.digits.length)
                    this.digits[i] = nextDigit(0);
                break;
            }
        }
        return value;
    }
    private int nextDigit(int minDigit) {
        for (int digit = minDigit; digit < 10; digit++)
            if (! this.inUse[digit]) {
                this.inUse[digit] = true;
                return digit;
            }
        return -1;
    }
}

答案 1 :(得分:0)

由于您不允许重复数字,因此很难直接生成您之后的列表。我认为最简单的方法是首先以字典顺序获取所有n长度组合,然后获得每个组合的所有排列。也就是说,对于n = 2情况,您将生成"01", "02", ... , "78", "79", "89",然后将获得每个的所有排列。

这是一个快速的程序,我把它放在一起,可以让你得到你想要的东西:

public static List<String> getCombinations (String digits, int n) {

  List<String> out = new ArrayList<String>();
  for ( int k = 0; k < digits.length (); ++k ) {
    if ( n == 1 )
      out.add (digits.charAt (k) + "");
    else {
      for ( String s : getCombinations (digits.substring (k + 1), n - 1) ) {
        out.add (digits.charAt (k) + s);
      }
    }
  }

  return out;
}


public static List<String> getPermutations (String s, String prefix) {

  List<String> out = new ArrayList<String>();

  if ( s.length () == 1 ) {
    out.add (prefix + s);
  }
  else {
    for ( int i = 0; i < s.length (); ++i ) {
      out.addAll (getPermutations (prefix + s.charAt (i), s.substring (0, i) + s.substring (i + 1)));
    }
  }

  return out;
}


public static List<String> getPermutations (List<String> combinations) {
  List<String> out = new ArrayList<String>();
  for ( String s : combinations )
    out.addAll (getPermutations (s, ""));

  return out;
}


public static void main (String[] args) {

  List<String> combinations = getCombinations ("0123456789", 3);

  List<String> permutations = getPermutations (combinations);

  for ( String s : permutations ) {
    System.out.println (s);
  }

}
相关问题