当前位置:网站首页>Full Permutation V3 recursion of brute force method /1~n
Full Permutation V3 recursion of brute force method /1~n
2022-06-10 21:09:00 【xcrj】
package xcrj.kchalgorithm.bruteForce;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** * Input n * give 1~n All permutations of * Power set problem ( Combination question ): Incremental greed , Increment without considering the order * The problem of permutation : Incremental greed , Consider the order of increment , The order is represented by the index * <p> * Incremental brute force method * 1 * 12 | 21 * 123 312 132 | 321 231 213 * <p> * recursive : * Recursive export : The number to be added i> The maximum number n * Recursive body : Give the original arrangement each time Index based j increase i produce 1 New permutations */
public class FullPermutation3 {
static Set<List<Integer>> setList = new HashSet<>(6);
static {
// initialization , Insert empty collection
List<Integer> list = new ArrayList<>(1);
setList.add(list);
}
/* stay setList Set List No j Position insert i*/
public static void insertI(List<Integer> list, int i, Set<List<Integer>> setListTemp) {
// stay setList Set List No j Position insert i
for (int j = 0; j < i; j++) {
List<Integer> listTemp = new ArrayList<>(3);
// Deep copy list To listTemp
for (Integer v : list) {
listTemp.add(v);
}
listTemp.add(j, i);
setListTemp.add(listTemp);
}
}
public static void fullPermutation3(int i, int n) {
if (i <= n) {
Set<List<Integer>> setListTemp = new HashSet<>(6);
for (List<Integer> list : setList) {
insertI(list, i, setListTemp);
}
// Deep copy setListTemp To setList
setList.clear();
for (List<Integer> listTemp : setListTemp) {
List<Integer> list = new ArrayList<>(3);
for (Integer v : listTemp) {
list.add(v);
}
setList.add(list);
}
// add to i+1
fullPermutation3(i + 1, n);
}
}
public static void main(String[] args) {
fullPermutation3(1, 3);
System.out.println(setList);
}
}
边栏推荐
- CET-6 - Business English - the last recitation before the test
- app測試用例
- pytorch深度学习——卷积操作以及代码示例
- 自注意力(self-attention)和多头注意力(multi-head attention)
- 堆排序与加强堆代码 用于记忆
- 记录一下今天的MySQL故障
- 用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
- 编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
- 蛮力法/1~n的全排列 v3 递归
- 揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
猜你喜欢

Four methods to obtain the position index of the first n values of the maximum and minimum values in the list

Canvas advanced functions (Part 1)

Magic tower game implementation source code and level generation

CET-6 - Business English - the last recitation before the test

Theoretical basis of distributed services

冷酸灵,一个国产品牌IPO的30年曲折史

LeetCode:497. 非重叠矩形中的随机点————中等

Self attention and multi head attention

Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解

LeetCode:497. Random points in non overlapping rectangles -- medium
随机推荐
Read the source code of micropyton - add the C extension class module (3)
蛮力法/u到v是否存在简单路径
【电脑使用】如何设置没有自启项的软件开机启动
leetcode 划分数组使最大差为 K
AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
Explain L3 cache to solve circular dependency
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
[computer use] how to set software startup without auto startup
LeetCode:1037. Effective boomerang - simple
Redis缓存击穿
保姆级教程:如何成为Apache Linkis文档贡献者
解决idea超过5个相同包的时候自动变成*的问题
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
Identity and access management (IAM)
pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)
AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
Is Jiuzhou futures regular? Is it safe to open an account
Deploying static websites using OSS and CDN on Alibaba cloud international
Self attention and multi head attention