当前位置:网站首页>Full Permutation Code (recursive writing)

Full Permutation Code (recursive writing)

2022-07-05 05:38:00 Java full stack R & D Alliance

The code is as follows :

public static List<String> getSubstring(String[] words) {
    
        if(words.length==1){
    
            return Arrays.asList(words);
        }
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
    
            String[] strings = Arrays.copyOf(words, words.length);
            strings = changeArray(strings, i);
            List<String> stringList = getSubstring(strings);
            for (String str : stringList) {
    
                resultList.add(words[i] + str);
            }
        }
        return resultList;
    }

    public static String[] changeArray(String[] words, int i) {
    
        for (int k = i; k < words.length - 1; k++) {
    
            words[k] = words[k + 1];
        }
        return Arrays.copyOf(words, words.length - 1);
    }

If you test

String[] words = {
    "1", "2", "3"};
List<String> substring = getSubstring(words);
System.out.println(substring);

Print the results :
 Insert picture description here

原网站

版权声明
本文为[Java full stack R & D Alliance]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050530439076.html