(待解决,效率低下)47. Permutations II C++回溯法

思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果

但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的vector中时,会出现memory limit exceeded错误(原因??)

上网查找后发现可以直接通过return vector<vector<int>> (mySet.begin(),mySet.end())得到结果,并且代码通过。

class Solution {
public:
    void backTrack(vector<int> nums, set<vector<int>>& mySet, vector<int> res, int k, int m[])
{
    if(k == nums.size())
    {
        mySet.insert(res);
    }
    else
    {
        for(int i=0;i<nums.size();i++)
        {
            if(!m[i])
            {
                m[i] = 1;
                res.push_back(nums.at(i));
                backTrack(nums,mySet,res,k+1,m);
                res.pop_back();
                m[i] = 0;
            }
        }
    }
}
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> ans;
        vector<int> res;
        set<vector<int>> mySet;
        int m[nums.size()] = {0,};//true为未用过
        backTrack(nums,mySet,res,0,m);
        /*set<vector<int>>::iterator itr = mySet.begin();
        while(itr != mySet.end())
        {
            ans.push_back(*itr);
            for(int i=0; i<(*itr).size();i++)
            {
                cout << (*itr).at(i) << ‘ ‘;
            }
            cout << endl;
            itr++;
        }*/
        return vector<vector<int>> (mySet.begin(),mySet.end());
    }
};

原文地址:https://www.cnblogs.com/tornado549/p/9990678.html

时间: 2024-10-28 16:40:38

(待解决,效率低下)47. Permutations II C++回溯法的相关文章

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

leetCode 47.Permutations II (排列组合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], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

47. 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], and [2,1,1]. 链接: http://leetcode.com/problems/permutations-ii/ 4/15

47. Permutations II java solutions

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] ] 1 public class Solution { 2 List<List<Integer>> ans

LeetCode 【47. 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] ] 思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复

47. Permutations II (java )

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. 解析: 编码:

[leedcode 47] 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], and [2,1,1]. public class Solution { List<Integer> seq; List<List&l

47. 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] ] 与上一题不同,就是在19行加个判断即可. 1 class Solution(object): 2 def __ini

19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&