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去遍历所有可能的序列,data记录结果序列中第i个数在num中的位置,sign记录深度遍历中num中每个值是否已经使用过,防止重复遍历。
当index=n时,表示遍历完,用result存当前符合条件的序列。
class Solution {
private:
vector >result;
int data[10];
int sign[10];
public:
vector
> permute(vector &num) {
result.clear();
memset(sign,0,sizeof(sign));
int n=num.size();
dfs(n,0,num);
return result;
}
void dfs(int n,int index,vector &num)
{
if(index==n)
{
vectort;
for(int i=0;i