Permutations II

Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]
解题技巧:

该题与Permutation基本相同,不同之处在于这道题中输入数组中可能包含重复数字,若采用Permutation的解法,会产生重复的结果。因此,需要在Permutation的解法中加一些处理:首先,将输入数组排序,保证重复的数字相邻,然后在递归函数中保证相同的元素按序被访问,也就是判断当前的数与前一个数是否相等,如果相等,前面的数必须已经使用了,当前的数字才能使用,否则需要跳过,这样就不会产生重复排列了。

代码:

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

vector< vector<int> > res;
bool *flag;

void dfs(vector<int>& nums, vector<int> res0)
{
    int len = nums.size();

    if(res0.size() == len )
    {
        res.push_back(res0);
        return;
    }

    for(int i = 0; i < len; i ++)
    {
        if(flag[i] == true) continue;
        if (i > 0 && nums[i] == nums[i - 1] && flag[i-1] == false ) continue; //通过限定相同数字的使用次序,避免重复序列

        flag[i] = true;
        res0.push_back(nums[i]);
        dfs(nums, res0);
        res0.pop_back();
        flag[i] = false;
    }
}

vector< vector<int> > permuteUnique(vector<int>& nums)
{
     vector<int> res0;
     flag = new bool [nums.size()];
     for(int i = 0; i < nums.size(); i ++)
     {
        flag[i] = false;
     }
     sort(nums.begin(), nums.end()); //排序,使得相同的数字相邻
     dfs(nums, res0);
     return res;
}

int main()
{
    int num;
    vector<int> nums;

    while(cin>>num)
    {
        nums.push_back(num);
    }

    res = permuteUnique(nums);

    for(int i = 0 ; i < res.size(); i ++)
    {
        for(int j = 0; j < res[i].size(); j ++)
        {
            cout<<res[i][j]<<' ';
        }
        cout<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值