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);
                }
            }
        }
    };
    

      

  • 相关阅读:
    English trip -- VC(情景课)1 A Get ready
    隔板法总结
    CF 题目选做
    ZROI 提高十连测 DAY2
    2019 09 05
    线性基总结
    解决痛苦的方法/cy
    梅深不见冬 树上贪心
    ZROI 提高十连测 Day1
    [USACO09NOV]硬币的游戏 博弈 dp
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3594841.html
Copyright © 2011-2022 走看看