全排列的代码 (递归写法)

关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述全排列的代码 (递归写法)相关的知识,希望能为你提供帮助。


代码如下:

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);

如果测试
String[] words = "1", "2", "3";
List< String> substring = getSubstring(words);
System.out.println(substring);

打印结果:

【全排列的代码 (递归写法)】



    推荐阅读