generating Variations without repetitions / Permutations in java

后端 未结 10 966
半阙折子戏
半阙折子戏 2021-01-02 07:33

I have to generate all variations without repetitions made of digits 0 - 9.

Length of them could be from 1 to 10. I really don\'t know how to solve it, especially ho

10条回答
  •  礼貌的吻别
    2021-01-02 08:00

    using Dollar it is simple:

    @Test
    public void generatePermutations() {
        // digits is the string "0123456789"
        String digits = $('0', '9').join();
    
        // then generate 10 permutations
        for (int i : $(10)) {
            // shuffle, the cut (0, 4) in order to get a 4-char permutation
            System.out.println($(digits).shuffle().slice(4));
        }
    }
    

提交回复
热议问题