LeetCode47: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].

这道题是打印一个整型数组的所有组合的问题。并且数组中可能有重复元素,之前在

字符串排序与八皇后问题这篇博客中就详细讨论了字符串排序的各种问题,这里就不在分析了。直接上代码:

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> result;
        permute(nums,0,result);
        return result;
    }

    void permute(vector<int> & nums,int offset,vector<vector<int>> & result)
    {
        //当最后需要排列的元素只剩下一个时即终止递归,将此时的nums存储到结果集result中。
        if(nums.size()==offset+1)
        {
            result.push_back(nums);
            return ;
        }

        //base指针用来指向需要递归的部分的首地址,iter指针是个变化的指针
        vector<int>::iterator base=nums.begin()+offset;//base的起始是offset
        vector<int>::iterator iter=base;//iter的起始是base,这里不要加上1,不然下面就没法递归了
        for(;iter!=nums.end();iter++)
        {
            //下面这个循环是用来判断是否有重复元素的关键
            vector<int>::iterator tmp;
            bool flag=true;
            for(tmp=base;tmp!=iter;tmp++)
            {
                if(*tmp==*iter)
                     flag=false;
            }
            if(flag)
            {
               swap(*base,*iter);//stl中本身就有swap函数,所以不需要自己实现了
               permute(nums,offset+1,result);
               swap(*iter,*base);
            }
        }
    }
};

runtime:32ms

需要注意的是一些编程细节方面的问题,比如迭代器的起始位置到什么地方终止。

时间: 2024-10-12 17:08:34

LeetCode47:Permutations II的相关文章

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], and [2,1,1]. 思路:递归地将每个元素与第一个元素进行交换.有一个额外的空间Set<Integer> duplicated,如果某

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] LeetCode47 II Given a collection of numb

LeetCode 047 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], and [2,1,1]. 代码如下: class Solution { public: vector

Permutations II leetcode java

题目: 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]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

【LeetCode】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], and [2,1,1]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

[LeetCode][JavaScript]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], and [2,1,1]. https://leetcode.com/problems/permutations

【leetcode刷题笔记】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]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上