简体   繁体   中英

Editing recursive algorithm: passing array instead of string as an argument to save results instead of printing

I have this code for finding n combinations out of an array with length k:

class Util
{
    // Function to print all distinct combinations of length k
    public static void recur(int[] A, String out, int n, int k)
    {
        // invalid input
        if (k > n) {
            return;
        }

        // base case: combination size is k
        if (k == 0) {
            System.out.println(out);
            return;
        }

        // start from next index till first index
        for (int i = n - 1; i >= 0; i--)
        {
            // add current element A[i] to output and recur for next index
            // (i-1) with one less element (k-1)
            recur(A, (A[i]) + " " + out, i, k - 1);
        }
    }

    public static void main(String[] args)
    {
        int[] A = {0, 1, 2, 3 };
        int k = 2;
        // process elements from right to left
        recur(A, "", A.length, k);
    }
}

it works fine and its main method prints

2 3 
1 3 
0 3 
1 2 
0 2 
0 1 

However I want to save these combinations in a list: List<int[]> or List<List<Integer>> . I tried to edit the algorithm:

public static void recur(int[] A, List<Integer> out, int n, int k)
    {
        // invalid input
        if (k > n) {
            return;
        }

        // base case: combination size is k
        if (k == 0) {
            System.out.println(out);
            return;
        }

        // start from next index till first index
        for (int i = n - 1; i >= 0; i--)
        {
            out.add(A[i]);
            // add current element A[i] to output and recur for next index
            // (i-1) with one less element (k-1)
            recur(A, out, i, k - 1);
        }
    }

but it doesn't work as expected: it prints

[3, 2]
[3, 2, 1]
[3, 2, 1, 0]
[3, 2, 1, 0, 2, 1]
[3, 2, 1, 0, 2, 1, 0]
[3, 2, 1, 0, 2, 1, 0, 1, 0]

for this main method:

public static void main(String[] args)
        {
            int[] A = {0, 1, 2, 3 };
            int k = 2;
            recur(A, new ArrayList<>(), A.length, k);
        }

The first case with String ou t works because String is immutable so that you can pass it without hurting the original.

The second case with ArrayList won't work because you pass a reference and when you modify the content of the "reference" you modify the original.

You are missing the Unchoose part of Choose - Explore - Unchoose approach in a typical backtracking.

Your Choose part is out.add(A[i])

Your Explore part is recur(A, out, i, k - 1)

Your Unchoose part should be to remove the element that you last chose ie the last element of the list : out.remove(out.size()-1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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