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].
用swap为了方便、简洁。
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main()
vector<int> answer;
vector<vector<int> > result;
dfs(result,answer,num,0);
return result;
}
void dfs (vector<vector<int> > &result, vector<int> &answer, vector<int> &num, int depth)
{
if(depth==num.size())
{
result.push_back(num);
return;
}
for(int i=depth;i<num.size();i++)
{
swap(num[depth],num[i]);
dfs(result,answer,num,depth+1);
swap(num[depth],num[i]);
}
}
};
6246

被折叠的 条评论
为什么被折叠?



