leetcode---permutations---dfs
2017-09-22 12:02
337 查看
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].
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].
class Solution { public: void dfs(int dep, int n, vector<vector<int> > &ans, vector<int> &num) { if(dep >= n) { ans.push_back(num); return; } for(int i=dep; i<n; i++) { swap(num[i], num[dep]); dfs(dep+1, n, ans, num); swap(num[i], num[dep]); } } vector<vector<int> > permute(vector<int> &num) { int n = num.size(); vector<vector<int> > ans; dfs(0, n, ans, num); return ans; } };
相关文章推荐
- [LeetCode]46. Permutations &&深搜DFS
- leetcode---permutations-ii---dfs
- [LeetCode]Permutations(回溯&&DFS&&递归!!!!)
- [LeetCode]47. Permutations II && DFS及剪枝
- leetcode | Permutations | 利用深度优先(DFS)的方法排列组合列表
- LeetCode-46-Permutations(DFS)-Medium
- Permutations(排列问题,DFS回溯)
- 488. Zuma Game(leetcode DFS hard)
- [LeetCode]Permutations
- leetcode -- Palindrome Partitioning -- 重点 dfs
- BFS和DFS解决LeetCode133. Clone Graph
- 47. Permutations II leetcode
- leetcode -- Copy List with Random Pointer -- deep copy问题,dfs
- LeetCode——Permutations
- LeetCode笔记:46. Permutations
- leetcode-46-permutations 搜索
- leetcode 140:Word Break II 采用很巧妙的动态规划和DFS联合使用 JAVA源代码实现
- leetcode 212 【trie+dfs】
- [LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence
- leetcode 46. Permutations