关闭

leetcode 47 : Permutations II

标签: 算法面试leetcode
287人阅读 评论(0) 收藏 举报
分类:

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 {
    vector > ret;
    int N;
    
public:
    void perm(vector &nums, int i){
        if( i == N){
            ret.push_back(nums);
        }
        set setNums;
        for(int j = i; j < N; j++){
            //没有交换过的元素才交换
            if( setNums.find(nums[j])==setNums.end() )
            {
                setNums.insert(nums[j]);
                swap(nums[i], nums[j]);
                perm(nums, i + 1);
                swap(nums[j], nums[i]);
            }
        }
    }

    vector> permuteUnique(vector& nums) {
        N = nums.size();
        ret.clear();
        perm(nums, 0);
        return ret;
    }
};
//next_permute
class Solution {
public:
    //不循环
    bool next_permute(vector& nums){
        if(nums.size()==0||nums.size()==1)
            return false;
        vector::iterator itF=nums.end()-2;
        vector::iterator itS=nums.end()-1;
        while(true)
        {
            if(*itF<*itS)
            {    
                vector::iterator tmpIt = nums.end()-1;
                while(*tmpIt<=*itF)
                    tmpIt--;
                int tmp=*itF;
                *itF=*tmpIt;
                *tmpIt=tmp;
                break;
            }
            if(itF==nums.begin())
            {
                return false;
            }
            itF--;
            itS--;
        }
        reverse(itS,nums.end());
        return true;
    }
    vector> permuteUnique(vector& nums) {
        vector> ret;
        sort(nums.begin(),nums.end());
        ret.push_back(nums);
        while(next_permute(nums))
            ret.push_back(nums);
        return ret;
    }
};
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

一次搞懂全排列——LeetCode四道Permutations问题详解

LeetCode中与Permutations相关的共有四题:   31. Next Permutation   46. Permutations   47. Permutations II  ...
  • Jacky_chenjp
  • Jacky_chenjp
  • 2017-03-26 17:52
  • 2414

【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】

【113-Path Sum II(路径和II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, find al...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-12 06:23
  • 2487

【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】

【219-Contains Duplicate II(包含重复元素II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-29 06:36
  • 3242

【LeetCode-47】Permutations II

这道题我是用递归来实现的,将数组的第1个元素和后面的所有元素看成是两部分,同理将第二部分的元素看成第一个元素和后面的所有元素两本.........很明显运用递归的思想!然后将第二部分的元素不断和第一个...
  • cold2wind
  • cold2wind
  • 2016-06-01 16:42
  • 129

LeetCode_47---Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...
  • bingbing8219
  • bingbing8219
  • 2015-06-24 14:16
  • 265

[LeetCode47]Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...
  • sbitswc
  • sbitswc
  • 2014-06-13 06:25
  • 608

<LeetCode OJ> 47/46 Permutations (II / I)

47. Permutations II My Submissions Question Total Accepted: 57990 Total Submissions: 215694 Diff...
  • EbowTang
  • EbowTang
  • 2016-01-09 21:03
  • 990

leetcode 47. Permutations II

import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.uti...
  • u011438605
  • u011438605
  • 2016-07-22 10:52
  • 155

LeetCode#47. Permutations II

数组元素排列组合
  • u012559634
  • u012559634
  • 2017-06-11 11:57
  • 100

LeetCode Algorithms 47. Permutations II

题目难度: Medium 原题描述: Given a collection of distinct numbers, return all possible permutations. For ...
  • u010985058
  • u010985058
  • 2017-06-01 21:01
  • 77
    个人资料
    • 访问:151143次
    • 积分:3148
    • 等级:
    • 排名:第12669名
    • 原创:154篇
    • 转载:115篇
    • 译文:0篇
    • 评论:3条
    最新评论