zoukankan      html  css  js  c++  java
  • Permutations

    Given a collection of numbers, return all possible permutations.

    For example,
    [1,2,3] have the following permutations:
    [1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

    简单递归题,同样没查最佳算法,今天还没看书呢……

    class Solution {
    public:
        vector<vector<int> > re;
        vector<vector<int> > permute(vector<int> &num) {
            vector<int> no;
            vector<int> count;
            sort(num.begin(),num.end());
            if(num.size()==0)
            return re;
    
            no.push_back(num[0]);
            count.push_back(1);
            int j = 0;
            for(int i = 1 ; i < num.size();i++)
            {
                if(num[i] == num[i-1])count[j]++;
                else
                {
                    no.push_back(num[i]);
                    j++;
                    count.push_back(1);
                }
            }
            vector<int>temp;
            per(no,count,num.size(),temp,-1);
            
        return re;
        }
        void per(vector<int> no , vector<int> count, int left,vector<int>now,int should)
        {
    
            int i=0;
            if(should != -1){
                now.push_back(no[should]);
                count[should]--;
                
            }
            if(left == 0)
            {
                re.push_back(now);
                return;
            }
    
            for(i=0;i<no.size();i++)
            {
                if(count[i]>0)
                {
                    per(no,count,left-1,now,i);
                }
            }
        }
    };
    

      

  • 相关阅读:
    HDU 1269 迷宫城堡 tarjan算法求强连通分量
    hrbust 1721 A + B = 0 map的应用
    关于vis标记
    poj 1703
    poj1961 kmp
    acm poj1260 dp
    矩阵链乘 hrbust 1600
    单源最短路径 hdu 2066
    最小生成树
    多维背包 hrbudt 1335 算法与追MM
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3594841.html
Copyright © 2011-2022 走看看