当前位置:网站首页>全排列递归思路整理
全排列递归思路整理
2022-07-27 09:08:00 【华为云】
前言
全排列,full permutation, 可以利用二叉树的遍历实现。二叉树的递归遍历,前中后都简洁的难以置信,但是都有一个共同特点,那就是一个函数里包含两次自身调用。
所以,如果一个递归函数中包含了两次自身调用,那么这类问题就是归纳成二分问题。也就是to be or not to be , is the problem。如果一个使用相同手段并且每一个点上可分为两种情况的问题,基本都可以转化为递归问题。当然,如果是有三个孩子的树,那么我们可能需要在一个递归函数中调用自身三次。
这里的递归,和我们计算的阶乘又有不一样,因为他没有返回,是发散的。也就是从一个节点,发散到N个节点,我们要的结果是叶子节点。
计算全排列,我们可以单独拿出每一个元素作为根节点来构成一棵树,所有的可能排列情况就都隐藏在森林中了。现在来看每一颗树,假如4个元素,A,B,C,D,以A为根是第一颗,表示以A开头的排列。
那么,第二个位置可以选着B,C,D,如果我们选择了B,那么B下还有 C, D可以选择, 如果我们选了C,那么最后只剩下D,这样,就列出第一种。如图所示:
我们可以看到,这里的孩子个数是递减的,直到最后一个元素,就不用选择了,同时也得到一种可能。
要枚举出所有的,那么就遍历这样一颗树。好了,先上代码。
边栏推荐
- Qdoublevalidator does not take effect solution
- NIO this.selector.select()
- CUDA Programming -03: thread level
- Pymongo fuzzy query
- Mangodb简单使用
- Deep understanding of Kalman filter (3): multidimensional Kalman filter
- Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
- 对 int 变量赋值的操作是原子的吗?
- async/await的执行顺序以及宏任务和微任务
猜你喜欢

Deep understanding of Kalman filter (1): background knowledge

NIO this.selector.select()

Redis network IO

杭州电子商务研究院发布“数字化存在”新名词解释

Pytorch custom CUDA operator tutorial and runtime analysis

Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic

ctfshow 终极考核

NIO this.selector.select()

Mangodb simple to use

Deep understanding of Kalman filter (2): one dimensional Kalman filter
随机推荐
[flutter -- geTx] preparation
E. Split into two sets
【ACL2020】一种新颖的成分句法树序列化方法
2034: [Blue Bridge Cup 2022 preliminary] pruning shrubs
vscod
NIO示例
Tensorflow loss function
[micro service ~sentinel] sentinel dashboard control panel
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
flex布局 (实战小米官网)
Restful
Matlab求解微分代数方程 (DAE)
Pyqt5 rapid development and practice 4.1 qmainwindow
Understand various IOU loss functions in target detection
qt中使用sqlite同时打开多个数据库文件
Mangodb simple to use
Sharing of four open source face recognition projects
TensorFlow损失函数
Encountered 7 file(s) that should have been pointers, but weren‘t
