当前位置:网站首页>全排列的代码 (递归写法)
全排列的代码 (递归写法)
2022-07-05 05:31:00 【Java全栈研发大联盟】
代码如下:
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);
打印结果:
边栏推荐
- 常见的最优化方法
- SDEI初探-透过事务看本质
- Drawing dynamic 3D circle with pure C language
- sync. Interpretation of mutex source code
- 读者写者模型
- 服务熔断 Hystrix
- 每日一题-搜索二维矩阵ps二维数组的查找
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- [interval problem] 435 Non overlapping interval
猜你喜欢
TF-A中的工具介绍
Gbase database helps the development of digital finance in the Bay Area
Sword finger offer 05 Replace spaces
A new micro ORM open source framework
剑指 Offer 05. 替换空格
Fragment addition failed error lookup
Sword finger offer 04 Search in two-dimensional array
Improvement of pointnet++
[to be continued] [UE4 notes] L2 interface introduction
Sword finger offer 58 - ii Rotate string left
随机推荐
Haut OJ 1221: a tired day
PC寄存器
Solon Auth 认证框架使用演示(更简单的认证框架)
Sword finger offer 35 Replication of complex linked list
读者写者模型
To the distance we have been looking for -- film review of "flying house journey"
2020ccpc Qinhuangdao J - Kingdom's power
2017 USP Try-outs C. Coprimes
Haut OJ 1357: lunch question (I) -- high precision multiplication
Developing desktop applications with electron
【Jailhouse 文章】Jailhouse Hypervisor
A new micro ORM open source framework
Haut OJ 1218: maximum continuous sub segment sum
A problem and solution of recording QT memory leakage
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
How many checks does kubedm series-01-preflight have
Sword finger offer 05 Replace spaces
Csp-j-2020-excellent split multiple solutions
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Acwing 4300. Two operations