当前位置:网站首页>回溯法/解空间树 排列树
回溯法/解空间树 排列树
2022-06-11 14:26:00 【xcrj】

- 初始时选择第0索引结点
- 选择第i个元素之后形成的父节点,父节点的孩子结点,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点
package xcrj.kchalgorithm.backtracking;
import java.util.Arrays;
/** * 排列树 * 达到要求的排列,所有元素都在;把那个元素放到第1位,把那个元素放到第2位,依次类推 * <p> * 选择第i个元素之后形成的父节点,父节点的孩子结点,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点 */
public class PermutationTree {
/** * @param xs * @param i * @param len */
public static void permutationTree(int[] xs, int i, int len) {
// 到达叶子结点输出
if (i == len) {
System.out.println(Arrays.toString(xs));
}
// 到达非叶子结点,生成i结点的所有孩子结点j
else {
// !选择第i个元素之后形成的父节点,父节点的孩子结点,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点
for (int j = i; j < len; j++) {
// 交换,通过父结点的第i和第j元素交换,轮流把第j个元素放到第i位,形成子结点
int temp = xs[j];
xs[j] = xs[i];
xs[i] = temp;
// 深度优先遍历操作孩子结点
permutationTree(xs, i + 1, len);
// 交换回来
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);
}
}
边栏推荐
- In depth research and analysis report on global and Chinese diet food market
- How to manually package your own projects
- Is bone conduction earphone good for bone? Is bone conduction earphone harmful to the body?
- In depth research and analysis report on global and Chinese p-chlorotrifluoromethane Market
- Uniapp settings page Jump effect - navigateto switching effect - Global animationtype animation
- Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?
- MySQL create table error 1067 - invalid default value for 'update_ time‘
- In depth research and analysis report on global and Chinese octamethylcyclotetrasiloxane (D4) market
- Individual income tax rate table
- Hashicopy之nomad应用编排方案02
猜你喜欢

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

Is bone conduction earphone good for bone? Is bone conduction earphone harmful to the body?

North China pushed Yale hard, MIT won the first place in a row, and the latest 2023qs world university ranking was released

IC fresh Chinese cabbage price of 400000 yuan! Experienced experts who have worked for many years guide you how to choose an offer!

老虎国际季报图解:营收5263万美元 持续国际化布局

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

How to manually package your own projects

mysql高级语句

Uniapp settings page Jump effect - navigateto switching effect - Global animationtype animation

Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)
随机推荐
C. Mortal Kombat Tower(cf)dp
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
数字化转型项目做了多年,主架构师都绝望了:当初就不应该用外包!
Current situation and future development trend of global and Chinese transcranial magnetic stimulation coil Market from 2022 to 2028
In depth research and analysis report on global and Chinese p-chlorotrifluoromethane Market
Zhu Ping: in the post epidemic era, medical beauty operations should not only be distracted, but also reverse the routine
以 Log4j 为例,如何评估和划分安全风险
Repository Manager之Nexus配置yum仓库
树莓派知识大扫盲
Analyse approfondie de la conception du système relationnel du Groupe de cercles
Taking log4j as an example, how to evaluate and classify security risks
Architectural concept exploration: Taking the development of card games as an example
[team learning] task06:for, if, and while
Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
Qualcomm WLAN framework learning (29) -- 6GHz overview
[public class preview]: mxplayer Ott audio and video transcoding practice and optimization
清北力压耶鲁,MIT蝉联第一,2023QS世界大学排名最新发布
你违规了吗?
深度解读:分布式系统韧性架构压舱石OpenChaos
Extracting storage is the best memory method