有重复值的全排列

题目链接:https://leetcode-cn.com/problems/permutations-ii/
题目描述:
有重复值的全排列

题解:

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;

    void backTracking(vector<int>& nums, vector<bool>& used)
    {
        if(path.size() == nums.size())
        {
            result.push_back(path);
            return;
        }

        for(int i = 0; i < nums.size(); i++)
        {
            //跳过同层重复值
            if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false)
                continue;
            if(used[i] == false)
            {
                used[i] = true;
                path.push_back(nums[i]);
                backTracking(nums, used);
                path.pop_back();
                used[i] = false;
            } 
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<bool> used(nums.size(), false);
        sort(nums.begin(), nums.end());
        backTracking(nums, used);
        return result;

    }
};

上一篇:LeetCode-047-全排列 II


下一篇:A*,IDA*—高档次的暴搜