【问题标题】:How to generate combinations from char array?如何从 char 数组生成组合?
【发布时间】:2021-11-20 09:26:17
【问题描述】:

如何从 char 数组生成组合? 这是我的代码 https://anotepad.com/notes/icjsc5ct 我有一个包含 5 个字符的数组:a、b、c、d、e 我想生成 3 个字符的组合

  1. [a, b, c];
  2. [a, b, d];
  3. [b, c, d];
  4. [b, c, e];
  5. [c, d, e]

我的代码只能生成 2 个 3 个字符的组合:

  1. [a, b, c];
  2. [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返回char在样本字符串“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&lt;char[]&gt; generate02(int r)是算法整数组合的复制和修改代码。我真的不明白算法是如何工作的

【问题讨论】:

  • edit您的问题并在此处添加格式正确的代码。当你在做的时候,添加更多关于你到底在问什么的信息,例如。你在哪里卡住了,你当前的代码做了什么以及你期望它做什么。
  • 可以在help center 下的How do I format my posts using Markdown or HTML? 中找到有关如何格式化您的问题的信息。

标签: java algorithm char combinations


【解决方案1】:

如果这是您正在寻找的解决方案/示例,我可以向您推荐我创建的 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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    相关资源
    最近更新 更多