leetcode #46 in cpp
2016-05-28 04:32
357 查看
Given a collection of distinct numbers, return all possible permutations.
For example,
and
Solution:
recurrent structure:
permutation of [1,2,3] = [1 , permutation(2,3)] and [2, permutation[1,3]] and [3, permutation(1,2)];
Code:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> members;
find_permutation(members, nums, &res);
return res;
}
void find_permutation(vector<int> members, vector<int> nums, vector<vector<int>> *res){
if(nums.size() == 1){
members.push_back(nums[0]);
res->push_back(members);
return;
}
for(int i = 0; i < nums.size(); i ++){
int temp = nums[i];
members.push_back(temp);//separate one number out of the array
nums.erase(nums.begin() + i);
find_permutation(members, nums, res);//find the permutation of the rest of numbers
nums.insert(nums.begin() + i, temp);
members.pop_back();
}
};
};
If we modify the code a little bit, we could abandon the use of 'members' and use less memory by using an index indicating where the permutation begins.
Code :
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
find_permutation(0, nums, &res);
return res;
}
void find_permutation(int start, vector<int> nums, vector<vector<int>> *res){
if(start == nums.size() - 1){
res->push_back(nums);
return;
}
for(int i = start; i < nums.size(); i ++){
swap(nums[i], nums[start]);
find_permutation(start + 1, nums, res);
swap(nums[i], nums[start]);
}
};
};
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].
Solution:
recurrent structure:
permutation of [1,2,3] = [1 , permutation(2,3)] and [2, permutation[1,3]] and [3, permutation(1,2)];
Code:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> members;
find_permutation(members, nums, &res);
return res;
}
void find_permutation(vector<int> members, vector<int> nums, vector<vector<int>> *res){
if(nums.size() == 1){
members.push_back(nums[0]);
res->push_back(members);
return;
}
for(int i = 0; i < nums.size(); i ++){
int temp = nums[i];
members.push_back(temp);//separate one number out of the array
nums.erase(nums.begin() + i);
find_permutation(members, nums, res);//find the permutation of the rest of numbers
nums.insert(nums.begin() + i, temp);
members.pop_back();
}
};
};
If we modify the code a little bit, we could abandon the use of 'members' and use less memory by using an index indicating where the permutation begins.
Code :
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
find_permutation(0, nums, &res);
return res;
}
void find_permutation(int start, vector<int> nums, vector<vector<int>> *res){
if(start == nums.size() - 1){
res->push_back(nums);
return;
}
for(int i = start; i < nums.size(); i ++){
swap(nums[i], nums[start]);
find_permutation(start + 1, nums, res);
swap(nums[i], nums[start]);
}
};
};
相关文章推荐
- leetcode #45 in cpp
- C语言程序设计感想 电信15-2 张寻 150206220
- C++队列与优先队列
- C++ 句柄类
- C++ string 字符串 结尾 标志 \0
- c++上机实验6-2
- CRC32校验算法C语言版(查表法)
- 简化版的SHA1算法C语言版
- 2阶节IIR算法C语言源码
- C语言版 256点FFT算法
- C语言修炼日记——extern
- HDOJ 1236 with three methods using C++
- 项目:文件压缩与解压
- C++ 马克思手稿问题(暴力循环)
- c++小技巧,宏定义工厂函数
- leetcode_137 Single Number II
- 【LeetCode-189】Rotate Array(C++)
- LeetCode34. Search for a range优雅解法
- JAVA学习(13) JAVA/C++互相调用 以及多线程
- 软件开发实战(二)