简体   繁体   English

插入加号时查找数字的所有组合

[英]Find all combinations of a number when inserting a plus sign

I am studying practice algorithm questions and can not seem to solve this question. 我正在研究练习算法问题,似乎无法解决这个问题。 I am giving a number such as 1234 . 我给的数字是1234 I need to find the different combinations of the number when adding a + symbol into it. 向其中添加+符号时,我需要找到数字的不同组合。 There can be multiple plus symbols in the answer. 答案中可以有多个加号。 So results would look like 1+234 , 1+2+34 , 12+34 etc. I know how to find the different substrings but not how to add them up. 因此结果看起来像1+234 1+2+34 12+34等。我知道如何找到不同的子字符串,但不知道如何将它们加起来。 Here is what I have: 这是我所拥有的:

public static void findCombos(String string){
    List<String> substrings = new ArrayList<>();
    for( int i = 0 ; i < string.length() ; i++ )
    {
        for( int j = 1 ; j <= string.length() - i ; j++ )
        {
            String sub = string.substring(i, i+j);
            substrings.add(sub);
        }
    }
    System.out.println(substrings);
}

This just stores the different substrings if convert the number to a string. 如果将数字转换为字符串,则仅存储不同的子字符串。 How would I put these together to create the correct string. 我如何将它们放在一起以创建正确的字符串。 I was thinking recursion with prefixes but could not get that right. 我在想带前缀的递归,但做不到。

This is different than the permutation question because I am asked to keep the number in the same order but add + 's to it. 这与置换问题不同,因为要求我将数字保持相同的顺序,但要在其中加上+

You want to generate all possible divisions of argument arg . 您想生成参数arg所有可能的除法。 The argument arg can be split in arg.length - 1 points. 参数arg可拆分为arg.length - 1点。 Here I am using a boolean array ( divisors[N] ) to remember whether you want to split between characters arg[N] and arg[N + 1] ). 在这里,我使用一个布尔数组( divisors[N] )来记住是否要在字符arg[N]arg[N + 1]之间分割。 All possible versions of divisors array are generated during the recursive flow - when you have reached the end, you then do the string division, and save the result. divisors数组的所有可能版本都是在递归流程中生成的-当您到达末尾时,然后进行字符串分割,并保存结果。

public static void cover(final String arg) {
    final List<Set<String>> result = new ArrayList<>();

    final boolean[] divisors = new boolean[arg.length() - 1];
    processDivisors(divisors, 0, arg, result);

    System.out.println(result);
    // now the result contains the divisions, we can do something with it
    doSomethingWithResult(result);
}

private static void processDivisors(final boolean[] divisors,
                                    final int position,
                                    final String arg,
                                    /* out */ final List<Set<String>> result) {

    if (position == arg.length() - 1) {
        final Set<String> computedDivision = computeDivision(arg, divisors);
        result.add(computedDivision);
        return;
    }
    divisors[position] = true;
    processDivisors(divisors, position + 1, arg, result);
    divisors[position] = false;
    processDivisors(divisors, position + 1, arg, result);
}

private static Set<String> computeDivision(final String arg, final boolean[] divisors) {
    final Set<String> computedDivision = new TreeSet<>();
    int start = 0;
    for (int i = 0; i < divisors.length; ++i) {
        if (divisors[i]) {
            computedDivision.add(arg.substring(start, i + 1));
            start = i + 1;
        }
    }
    computedDivision.add(arg.substring(start, arg.length()));
    return computedDivision;
}

private static void doSomethingWithResult(final List<Set<String>> result) {
    for (final Set<String> coverage : result) {
        final String message = String.join("+", coverage);
        System.out.println(message);
    }
}

I strongly believe this code is not perfect, and could be somehow optimized. 我坚信此代码并不完美,并且可以以某种方式进行优化。 Btw. 顺便说一句。 if you need to do any extra operation when you do the division, you can modify the computeDivison method. 如果执行除法时需要执行任何其他操作,则可以修改computeDivison方法。 But certainly wouldn't use that part for printing - it would be wiser to have the output generated first, and then processed in a different segment of code. 但是肯定不会使用该部分进行打印-首先生成输出,然后在不同的代码段中进行处理会更明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM