How I can get all combinations that have duplicates in Java (recursion)?

前端 未结 1 819
温柔的废话
温柔的废话 2021-01-22 18:47

I need to find a way to remove duplicates from a combination like this:

Input: 3 and 2, where 3 is the range (from 1 to 3) and 2 is the length of each combinati

1条回答
  •  滥情空心
    2021-01-22 19:23

    You can pass the last max value used into gen:

    private static void gen(int index, int minI) {
        if (index == result.length) {
            printArray();
            return;
        }
        for (int i = minI; i <= n; i++) {
            result[index] = i;
            gen(index + 1, i);
        }
    }
    

    And call it starting with 1: gen(0, 1);

    0 讨论(0)
提交回复
热议问题