java - 如何从 char 数组生成组合?
问题描述
如何从 char 数组生成组合?这是我的代码 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 数组转换为字符串并显示它以供我调试
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 ); }
函数 IndexInt 返回样本字符串“abcde”中字符的位置(从 0 开始)
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 在示例字符串“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)
是算法整数组合的复制和修改代码。我真的不明白算法是如何工作的
解决方案
如果它是您正在寻找的解决方案/示例,我可以向您推荐我创建的 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]
推荐阅读
- html - 如何在底部带有标题的图像旁边放置文本
- python - OpenCV:同时从多个边界检测文本
- android - 如何在房间中使用@Relation
- javascript - 将计数器限制在 0 到 10 之间
- python - python beautifulsoup4 TypeError:“NoneType”对象不可调用
- kubernetes - 在 EKS 集群中创建 NodePort 服务时出错
- laravel - count():参数必须是一个数组或者是实现了 Countable on Laravel 关系的对象
- bash - “sed”与模式不匹配
- javascript - bundle.min.js 不在 html 文件中显示组件
- python - 使用pygame时,引入新线程时程序卡顿