如何从 char 数组生成组合?
How to generate combinations from char array?
如何从字符数组生成组合?
这是我的代码
https://anotepad.com/notes/icjsc5ct
我有一个包含 5 个字符的数组:a、b、c、d、e
我想生成 3 个字符的组合
- [a, b, c];
- [a, b, d];
- [b, c, d];
- [b, c, e];
- [c, d, e]
我的代码只能生成 3 个字符的 2 种组合:
- [a, b, c];
- [a, b, d];
我不知道如何增加Array[0]和Array[1]的索引;
public ArrayList< char[] > generate02( int r ) {
ArrayList< char[] > combinationsList = new ArrayList<>();
char[] data = new char[ r ];
// initialize with lowest lexicographic combination
for ( int i = 0; i < r; i++ ) {
data[ i ] = CharArray01[ i ];
}
PrintData( CharArray01 );
int n = CharArray01.length;
while ( IndexInt( data[ r - 1 ] ) < n - 1 ) {
int t01 = r - 1;
System.out.println( " IndexInt( data[ r - 1 ] ) < n " );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] )
+ "; n = " + n );
combinationsList.add( data.clone() );
// generate next combination in lexicographic order
int t02 = n - r + t01;
while ( t01 != 0 && IndexInt( data[ t01 ] ) == t02 ) {
t01--;
}
int k1 = IndexInt( data[ r - 1 ] );
int k2 = k1 + 1;
data[ r - 1 ] = IndexChar( k2 );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] ) );
System.out.println( "t01 = " + t01 + "; n = " + n );
int i = 0;
for ( i = t01 + 1; i < r; i++ ) {
int index02 = IndexInt( data[ i - 1 ] );
int index03 = index02 + 1;
data[ i ] = data[ index03 ];
}
}
return combinationsList;
}
```
函数 PrintData 将 char 数组转换为 String 并显示它用于我的调试
public void PrintData( char[] CharArray02 ) {
int length = CharArray02.length;
String string01 = "";
for ( int i = 0; i < length; i++ ) {
string01 = string01 + CharArray02[ i ] + " ";
}
System.out.println( "PrintData: String01 = " + string01 );
}
Function IndexInt return 字符(从 0 开始)在示例字符串“abcde”中的位置
public int IndexInt( char char01 ) {
int result = 0;
String string01 = "abcde";
int length = string01.length();
for ( int i = 0; i < length; i++ ) {
if ( char01 == (char) string01.charAt( i ) ) { return i; }
}
return result;
}
函数 IndexChar return 示例字符串“abcde”中 int 位置的字符
public char IndexChar( int index01 ) {
char result = 'a';
String string01 = "abcde";
result = string01.charAt( index01 );
return result;
}
函数public ArrayList<char[]> generate02(int r)
是算法整数组合的复制和修改代码。我真的不明白算法是如何工作的
如果它是您正在寻找的 solution/example,我可以向您推荐我创建的 API 以高效内存方式生成对象组合:https://github.com/3venthorizon/meta/blob/master/meta-mathics/src/main/java/com/devlambda/meta/mathics/CombinationGenerator.java
@Test
public void charComboTest() {
List<Character> domain = Arrays.asList('a', 'b', 'c', 'd', 'e');
CombinationGenerator<Character> combinations = new CombinationGenerator<>(domain, 3);
while (combinations.hasNext()) {
List<Character> combination = combinations.next();
System.out.println(combination.stream().map(Object::toString)
.collect(Collectors.joining(", ", "[", "]")));
}
}
这将打印出以下结果:
[a, b, c]
[a, b, d]
[a, b, e]
[a, c, d]
[a, c, e]
[a, d, e]
[b, c, d]
[b, c, e]
[b, d, e]
[c, d, e]
如何从字符数组生成组合? 这是我的代码 https://anotepad.com/notes/icjsc5ct 我有一个包含 5 个字符的数组:a、b、c、d、e 我想生成 3 个字符的组合
- [a, b, c];
- [a, b, d];
- [b, c, d];
- [b, c, e];
- [c, d, e]
我的代码只能生成 3 个字符的 2 种组合:
- [a, b, c];
- [a, b, d];
我不知道如何增加Array[0]和Array[1]的索引;
public ArrayList< char[] > generate02( int r ) {
ArrayList< char[] > combinationsList = new ArrayList<>();
char[] data = new char[ r ];
// initialize with lowest lexicographic combination
for ( int i = 0; i < r; i++ ) {
data[ i ] = CharArray01[ i ];
}
PrintData( CharArray01 );
int n = CharArray01.length;
while ( IndexInt( data[ r - 1 ] ) < n - 1 ) {
int t01 = r - 1;
System.out.println( " IndexInt( data[ r - 1 ] ) < n " );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] )
+ "; n = " + n );
combinationsList.add( data.clone() );
// generate next combination in lexicographic order
int t02 = n - r + t01;
while ( t01 != 0 && IndexInt( data[ t01 ] ) == t02 ) {
t01--;
}
int k1 = IndexInt( data[ r - 1 ] );
int k2 = k1 + 1;
data[ r - 1 ] = IndexChar( k2 );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] ) );
System.out.println( "t01 = " + t01 + "; n = " + n );
int i = 0;
for ( i = t01 + 1; i < r; i++ ) {
int index02 = IndexInt( data[ i - 1 ] );
int index03 = index02 + 1;
data[ i ] = data[ index03 ];
}
}
return combinationsList;
}
```
函数 PrintData 将 char 数组转换为 String 并显示它用于我的调试
public void PrintData( char[] CharArray02 ) { int length = CharArray02.length; String string01 = ""; for ( int i = 0; i < length; i++ ) { string01 = string01 + CharArray02[ i ] + " "; } System.out.println( "PrintData: String01 = " + string01 ); }
Function IndexInt return 字符(从 0 开始)在示例字符串“abcde”中的位置
public int IndexInt( char char01 ) { int result = 0; String string01 = "abcde"; int length = string01.length(); for ( int i = 0; i < length; i++ ) { if ( char01 == (char) string01.charAt( i ) ) { return i; } } return result; }
函数 IndexChar return 示例字符串“abcde”中 int 位置的字符
public char IndexChar( int index01 ) { char result = 'a'; String string01 = "abcde"; result = string01.charAt( index01 ); return result; }
函数
public ArrayList<char[]> generate02(int r)
是算法整数组合的复制和修改代码。我真的不明白算法是如何工作的
如果它是您正在寻找的 solution/example,我可以向您推荐我创建的 API 以高效内存方式生成对象组合:https://github.com/3venthorizon/meta/blob/master/meta-mathics/src/main/java/com/devlambda/meta/mathics/CombinationGenerator.java
@Test
public void charComboTest() {
List<Character> domain = Arrays.asList('a', 'b', 'c', 'd', 'e');
CombinationGenerator<Character> combinations = new CombinationGenerator<>(domain, 3);
while (combinations.hasNext()) {
List<Character> combination = combinations.next();
System.out.println(combination.stream().map(Object::toString)
.collect(Collectors.joining(", ", "[", "]")));
}
}
这将打印出以下结果:
[a, b, c]
[a, b, d]
[a, b, e]
[a, c, d]
[a, c, e]
[a, d, e]
[b, c, d]
[b, c, e]
[b, d, e]
[c, d, e]