Java 将大小为n的所有二进制字符串生成为布尔数组的最快方法?

Java 将大小为n的所有二进制字符串生成为布尔数组的最快方法?,java,binary,boolean,Java,Binary,Boolean,例如,如果我想要所有长度为3的二进制字符串,我可以这样简单地声明它们: boolean[] str1 = {0,0,0}; boolean[] str2 = {0,0,1}; boolean[] str3 = {0,1,0}; boolean[] str4 = {0,1,1}; boolean[] str5 = {1,0,0}; boolean[] str6 = {1,0,1}; boolean[] str7 = {1,1,0}; boolean[] str8 = {1,1,1}; 将长度为N

例如,如果我想要所有长度为3的二进制字符串,我可以这样简单地声明它们:

boolean[] str1 = {0,0,0};
boolean[] str2 = {0,0,1};
boolean[] str3 = {0,1,0};
boolean[] str4 = {0,1,1};
boolean[] str5 = {1,0,0};
boolean[] str6 = {1,0,1};
boolean[] str7 = {1,1,0};
boolean[] str8 = {1,1,1};
将长度为N的所有可能的二进制字符串生成为布尔数组的最有效方法是什么

我不一定需要最有效的方法,只需要一个对我来说相当有效且易于多线程的方法


编辑:我应该注意,如果有必要的话,我将把它们都存储在一个ArrayList中。

下面是一些生成真值表的代码。。。(由于数组大小限制,仅适用于32位(如果需要,可以将大小变量更改为任意值并将布尔值存储为1/0):

int size=3;
int numRows=(int)Math.pow(2,size);
布尔值[][]布尔值=新布尔值[numRows][size];
对于(int i=0;i>>j));
bools[i][j]=ret!=0;
系统输出打印(bools[i][j]+“\t”);
}
System.out.println();
}

在函数中实现它-

static public ArrayList<boolean[]> getBoolArr(int length) {
        int numOptions = 1 << length;
        ArrayList<boolean[]> finalArray = new ArrayList<boolean[]>();
        for(int o=0;o<numOptions;o++) {
            boolean[] newArr = new boolean[length];
            for(int l=0;l<length;l++) {
                int val = ( 1<<l ) & o;
                newArr[l] = val>0;
            }
            finalArray.add(newArr);
        }
        return finalArray;
    }
static public ArrayList getBoolArr(int-length){

int numOptions=1如果你不想一次完成所有的排列,那么明智的做法是事先不分配内存,只需编写一个算法,即可计算出你想要的
strX

这样做的好处:

  • 您可以处理任意数量的排列,而无需分配所有排列
  • 由于算法不存储任何内容,因此它是线程友好的
  • 您只需为所需的行付费。例如,如果n=1000,但您只需要少数排列,则速度会快得多,并且只需要一小部分内存(仅相当于一行)
开始时,算法的界面可以如下所示:

boolean[] str1 = {0,0,0};
boolean[] str2 = {0,0,1};
boolean[] str3 = {0,1,0};
boolean[] str4 = {0,1,1};
boolean[] str5 = {1,0,0};
boolean[] str6 = {1,0,1};
boolean[] str7 = {1,1,0};
boolean[] str8 = {1,1,1};
布尔[]getRow(整数行数,整数行数)


因此,您可以调用
getRow(5,3)
来获取从函数返回的
str5
。具体细节由您来实现(这并不难)。

示例:如果需要长度为4的数组,则必须有2^4=16个不同的数组

您可以使用以下简单Java代码生成所有数组:

for (int i=0; i < (Math.pow(2,4)); i++) {
        System.out.println(Integer.toBinaryString(i));
}
for(inti=0;i<(Math.pow(2,4));i++){
System.out.println(Integer.toBinaryString(i));
}
该系统的输出:

01110110010111110001011011100111011111


javascript
与重复问题相关的实现

与所有数字一样,数字集之间也存在关系,一旦识别出模式,就可以使用加法来推导数组集中特定索引处的数字集之间的关系

让[N,n1,n2,n3]=[0,1,9,89];
设[res,max]=[Array(Array(3.fill(N)),Math.pow(2,3)];
对于(设i=1,curr;in==i)){
N+=n1;
}
如果([2,6].some(n=>n==i)){
N+=n2;
}
如果(i==最大值/2){
N+=n3;
}
curr=Array.from(字符串(N),N=>+N);
如果(N<100){
而(当前长度<3){
当前未移位(n1-1);
}
}
推力(电流);
}

console.log(res);
这就是我在Java中所做的

public class NbitsStrings {
    int[] arrA;
    public static void main(String[] args) throws java.lang.Exception {
        Scanner input = new Scanner(System.in); //Input the Number Of bits you want.
        int n = input.nextInt();
        NbitsStrings i = new NbitsStrings(n);
        i.nBits(n);
    }
    public NbitsStrings(int n) {
        arrA = new int[n];
    }
    public void nBits(int n) {
        if (n <= 0) {
            StringBuilder builder = new StringBuilder();
            for (int value : arrA) {
                builder.append(value);
            }
            String text = builder.toString();
            System.out.println(text);
        } else {
            arrA[n - 1] = 0;
            nBits(n - 1);
            arrA[n - 1] = 1;
            nBits(n - 1);
        }
    }
}
公共类nbitstrings{
int[]阿拉;
公共静态void main(字符串[]args)引发java.lang.Exception{
扫描仪输入=新扫描仪(System.in);//输入所需的位数。
int n=input.nextInt();
nbitstrings i=新的nbitstrings(n);
i、 nBits(n);
}
公共字符串(int n){
arrA=新整数[n];
}
公共非银行存款(int n){

如果(n这就是我在Scala中实现它的方式

def combinations(n: Int): List[List[Int]] = {
 val numbers = scala.math.pow(2, n).toInt 
 //Convert each to binary equivalent 
 def toBinary(decimal: Int, binary: List[Int]): List[Int] = {
  if (decimal <= 0)
    return le(binary)
  toBinary(decimal / 2, (decimal % 2) :: binary)
 }
 // Size alignment 
 def le(binary: List[Int]):List[Int]=
  {
    if(binary.length!=n) le(0::binary) else binary
  }
 def getCombinations(n: Int, list: List[List[Int]]): List[List[Int]] = {
  if (n < 0)
    return list
  getCombinations(n - 1, toBinary(n, Nil) :: list)
 }
 getCombinations(numbers - 1, Nil)
}
def组合(n:Int):List[List[Int]={
val numbers=scala.math.pow(2,n).toInt
//将每个转换为等效的二进制文件
def toBinary(十进制:Int,二进制:List[Int]):List[Int]={

if(decimal)对我不起作用。如果我使用小写作为长度,我会出现越界异常。如果我使用大数字,我会得到1位数的输出。我不理解所有这些,请再试一次,它会起作用。我将代码放在函数中,这样就不会出现错误(请参阅更新)。如果您有任何问题,请告诉我。'我喜欢这种方法。不过,我会再等一些。@Chickeneatergy:既然您说过要对其进行多线程处理,我在这里做的一个(或两个)微不足道的改进是:1)将打印内容移出循环,2)将I的类型更改为AtomicInteger(进行必要的调整).#2可以简单地适应多线程实现。由于这里的代码块很小,所以不确定这样做会获得多少好处。在线程上划分工作的更好方法可能是为每个线程在i上划分范围。@user314104:我做到了。速度很快。我使用了arraylist,但我使用了您的方法。谢谢!仅此而已将在n=31时失败
def combinations(n: Int): List[List[Int]] = {
 val numbers = scala.math.pow(2, n).toInt 
 //Convert each to binary equivalent 
 def toBinary(decimal: Int, binary: List[Int]): List[Int] = {
  if (decimal <= 0)
    return le(binary)
  toBinary(decimal / 2, (decimal % 2) :: binary)
 }
 // Size alignment 
 def le(binary: List[Int]):List[Int]=
  {
    if(binary.length!=n) le(0::binary) else binary
  }
 def getCombinations(n: Int, list: List[List[Int]]): List[List[Int]] = {
  if (n < 0)
    return list
  getCombinations(n - 1, toBinary(n, Nil) :: list)
 }
 getCombinations(numbers - 1, Nil)
}