当前位置:网站首页>Backtracking / solution space tree permutation tree
Backtracking / solution space tree permutation tree
2022-06-11 14:42:00 【xcrj】

- Initially select the No 0 Index node
- Select the first i Parent node formed after elements , The child node of the parent node , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes
package xcrj.kchalgorithm.backtracking;
import java.util.Arrays;
/** * Permutation tree * Achieve the required arrangement , All the elements are in ; Put that element in the 1 position , Put that element in the 2 position , By analogy * <p> * Select the first i Parent node formed after elements , The child node of the parent node , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes */
public class PermutationTree {
/** * @param xs * @param i * @param len */
public static void permutationTree(int[] xs, int i, int len) {
// Reach the leaf node output
if (i == len) {
System.out.println(Arrays.toString(xs));
}
// Reach the non leaf node , Generate i All child nodes of the node j
else {
// ! Select the first i Parent node formed after elements , The child node of the parent node , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes
for (int j = i; j < len; j++) {
// In exchange for , Through the parent node i And the j Element exchange , Take turns to j Put the first element in the second i position , Form child nodes
int temp = xs[j];
xs[j] = xs[i];
xs[i] = temp;
// Depth first traversal operation child node
permutationTree(xs, i + 1, len);
// Exchange back
int temp2 = xs[j];
xs[j] = xs[i];
xs[i] = temp2;
}
}
}
public static void main(String[] args) {
int[] xs = {
1, 2, 3};
permutationTree(xs, 0, xs.length);
}
}
边栏推荐
- 中国技术出海,TiDB 数据库海外探索之路 | 卓越技术团队访谈录
- 高通WLAN框架学习(29)-- 6GHz 概述
- Zhejiang University has developed a UAV, which can automatically avoid obstacles and walk through the woods like a bird. The real swarm is coming
- 2022 simulated 100 questions and simulated examination of quality officer municipal direction post skills (Quality Officer) examination
- Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
- 汤峥嵘:CTO 是商业思维和技术思维交汇的那个点
- Redis configuration and optimization of NoSQL
- Determine whether a string contains the specified string (verified)
- 化“被动”为“主动”,如何构建安全合规的智能产品 | Q推荐
- Did you break the rules?
猜你喜欢

In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology

Avenue to simplicity | how to configure self attention for vit is the most reasonable?

Analyse approfondie de la conception du système relationnel du Groupe de cercles

非常值得学习的调度开源库推荐

Seven parameters of thread pool and reject policy

Telecommuting with cpolar (1)

【clickhouse专栏】新建库角色用户初始化

Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)

Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)

简单的C语言版本通讯录
随机推荐
大道至簡 | 設計 ViT 到底怎麼配置Self-Attention才是最合理的?
mysql创建表出错1067 - Invalid default value for ‘update_time‘
【SystemVerilog 之 过程块和方法】~ 域、always过程块、initial过程块、函数 function、任务 task、生命周期
Guess numbers games
IC fresh Chinese cabbage price of 400000 yuan! Experienced experts who have worked for many years guide you how to choose an offer!
深度解读:分布式系统韧性架构压舱石OpenChaos
In depth research and analysis report on global and Chinese gas monitor market
清北力压耶鲁,MIT蝉联第一,2023QS世界大学排名最新发布
回溯法/活动安排 最大兼容活动
Repository Manager之Nexus
Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
基于STM32F1的开源小项目
Repository Manager之Nexus配置yum仓库
gensim.models word2vec 参数
一些经典的嵌入式C面试题汇总
Hamad application layout scheme 05 of hashicopy (visit the web page)
Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
Current situation and future development trend of precision air conditioning market in the world and China
Hashicopy之nomad应用编排方案01
Did you break the rules?