【LeetCode练习题】Permutations
全排列
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].
题目意思:
给定一个集合,求全排列。
解题思路:
一般的话,有两种思路可以考虑。递归和DFS遍历树。
这里我只用了递归的方法,关于怎么建一棵树然后DFS,可以参考 这里。
这对于练习对递归的理解真是个不错的题目。
以[1,2,3]举例,可以用纸笔画一画,分别以1,2,3为根,画出三棵树来,然后由根到每一个节点就对应着一个排列。
首先最外层肯定要有一个for循环来一次遍历这里面的元素决定出根节点,然后在循环中递归。
其中,有两点需要注意的:
- 要设置一个bool型的visited数组,大小和num一样大,来标记num中对应的元素是否已经访问过了,对于还没有访问过的,我们才可以把它加到element里来。这样可以避免在树的不同层出现同一个数。
- 在element已经完成了一次排列后,对element进行pop_back(),然后将pop出来的数对应的visited设置为true,表示它现在可以被访问了。举例来说,当element中已经有了[1,2,3]并且添加到result里后,进行一个pop_back()剩下[1,2]并将visited[2]置为true,然后跳到上一层调用,再pop_back()剩下[1]并将visited[1]置为true,此时for循环i++,此时visited[2]为true,即向element里添加num[2],element变成[1,3],接着再调用递归,在下一层因为visited[0]为false,visited[1]为true,element将添加num[1]变成[1,3,2]。
对递归不熟的朋友,可以一边对着代码,一边用笔画一下几个树,过几遍就OK拉!
代码如下:
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
vector<int> element; int len = num.size();
bool *visited = new bool[len];
memset(visited,false,len); helper(num,result,element,visited);
return result;
} private:
void helper(vector<int> &num,vector<vector<int>> &result,vector<int> &element,bool *visited){
if(element.size() == num.size()){
//element中元素和num中元素一样多,说明得到了一个全排列,放进result里
result.push_back(element);
return ;
} for(int i = ; i < num.size(); i++){
if(!visited[i]){
visited[i] = true;
element.push_back(num[i]);
helper(num,result,element,visited); element.pop_back();
visited[i] = false;
}
}
}
}; int main(){
Solution s;
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
s.permute(num);
}
【LeetCode练习题】Permutations的更多相关文章
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
随机推荐
- github page使用
github page介绍: https://help.github.com/categories/20/articles 使用GitHub Pages建立博客 与GitHub建立好链接之后,就可以方 ...
- mysql update语句
UPDATE ClientBankInfo SET status = 3 WHERE sn IN (SELECT sn FROM zjzc.ClientBankInfo WHERE cardNo IN ...
- [Ubuntu]在Ubuntu下搭建自己的源服务器
1.摘要 网上有很很多关于搭建源镜像的文章,但是对于一般人来讲,用不着镜像所有的deb包,只对我们能用到的感兴趣.另外,对于一些在Ubuntu的源中没有的软件,我们也可以放在我们自己的源里,这 ...
- < IOS > IOS适配,简单的分析解决一下
版权:张英堂 欢迎转载,转载请注明出处. 做的项目很多,一到适配的时候头就大了,IOS6,7的适配,屏幕的适配,当然还有下一步要出4.7屏幕,也要做适配....悲剧的移动端的人员. 怎么做一个通用的适 ...
- android系统的图片资源
使用系统的图片资源的好处有,一个是美工不需要重复的做一份已有的图片了,可以节约不少工时:另一个是能保证我们的应用程序的风格与系统一致. 1.引用方式 在源代码*.Java中可以进入如下方式引用: my ...
- 把程序嵌入网页之ATL编写ActiveX[标准窗口+接受参数]
从VS2010开始ATL ActiveX支持IObjectSafety接口,所以用VS2010来编写,新建一个ATL项目 向导的第一页没什么东西,直接下一步,选项可以根据具体需求调整 点“完成”,切换 ...
- Unix命令行学习
Listing files and directories ls 显示你当前目录的文件 ls -a 显示你千亩目录的文件(包括隐藏文件)ls <name1>/<name2> 显 ...
- UNIX环境高级编程--高级I/O(三)
一.高级I/O 包括非阻塞I/O.记录锁.系统V流机制.I/O多路回转(select和poll函数).readv和writev函数以及存储映射I/O(mmap),这些都是高级I/O. 其实在上面 ...
- 通过匹配绑定select option的文本值 模糊匹配
//通过匹配绑定select option的文本值 模糊匹配 $(".class option:contains('文本值')").attr("selected" ...
- HDU 1065 - I Think I Need a Houseboat
又是恶心人的水题 圆周率取3.1415926就啥事没有.. #include <iostream> #include <cstdio> #include <cmath&g ...