LeetCode (Permutations)
2017-05-03 12:47
381 查看
Problem:
Given a collection of distinct numbers, return all possible permutations.
For example,
Solution:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
if (nums.empty() || nums.size() == 1){
return {nums};
}
vector<int> num = nums;
num.erase(num.begin());
vector<vector<int>> b = permute(num);
for (int j = 0; j < b.size(); j++)
for (int l = 0; l < nums.size(); l++){
vector<int> p = b[j];
p.insert(p.begin() + l, nums[0]);
ans.push_back(p);
}
return ans;
}
};
Given a collection of distinct 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], [3,2,1] ]
Solution:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
if (nums.empty() || nums.size() == 1){
return {nums};
}
vector<int> num = nums;
num.erase(num.begin());
vector<vector<int>> b = permute(num);
for (int j = 0; j < b.size(); j++)
for (int l = 0; l < nums.size(); l++){
vector<int> p = b[j];
p.insert(p.begin() + l, nums[0]);
ans.push_back(p);
}
return ans;
}
};
相关文章推荐
- [LeetCode] Permutations
- LeetCode: Permutations
- 【LeetCode】Permutations && Combinations 排列组合
- [leetcode] Permutations
- 【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
- LeetCode - 46 - Permutations
- leetcode——46——Permutations
- LeetCode-46 Permutations(全排列)
- LeetCode - 46. Permutations
- LeetCode-46-Permutations(DFS)-Medium
- LeetCode--Permutations
- leetcode解题方案--046--Permutations
- Leetcode 46 Permutations
- [LeetCode] Permutations 解题报告
- LeetCode 46 Permutations (全排列)
- leetcode-46-permutations 搜索
- [LeetCode] Permutations
- LeetCode 数字排列问题 46 Permutations
- leetcode Permutations II
- [Leetcode] Permutations