• leetcode103:permutations-ii


    题目描述

    给出一组可能包含重复项的数字,返回该组数字的所有排列
    例如;
    [1,1,2]的排列如下:
    [1,1,2],[1,2,1], [2,1,1].

    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 {
        int array[19]={0};
        void permutation(vector<vector<int>> &ans ,vector<int> &num,int k,int n){
            if (k==n)
                ans.push_back(num);
            else
            {
                for (int i=0;i<19;i++){
                    if (array[i]>0)
                    {
                        array[i]--;
                        num[k]=i-9;
                        permutation(ans, num,  k+1,  n);
                        array[i]++;
                    }
                }
            }
        }
    public:
        vector<vector<int> > permuteUnique(vector<int> &num) {
            for (int i=0;i<num.size();i++){
                array[num[i]+9]++;
            }
            vector<vector<int>> ans;
            permutation(ans, num, 0, num.size());
            return ans;
        }
    };
  • 相关阅读:
    系统权限控制模型
    [Golang] 剑走偏锋 -- IoComplete ports
    Golang 正则匹配 -- regexp
    golang -- 字符串就地取反
    Hyperledger Fabric chaincode 开发(疑难解答)
    could not launch process: decoding dwarf section info at offset 0x0: too short
    win10 Ubuntu16 双系统
    7-8 哈利·波特的考试
    7-7 六度空间
    7-6 列出连通集
  • 原文地址:https://www.cnblogs.com/hrnn/p/13416387.html
Copyright © 2020-2023  润新知