码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode OJ-- 二战 Combinations

时间:2014-09-06 22:28:03      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   art   div   sp   

在1 - 10 中,求出 7 个数的排列组合。

出现了超时,而超时的原因是有好多重复情况,复杂度上来说,和答案的复杂度是一样的,但是答案中重复了太多了,体会下。

超时1:

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> > ans;
        if(n < k || n <= 0 || k <= 0)
            return ans;
        
        vector<int> ansPiece;
        set<int> checkExist;
        for(int i = 1; i <= n; i++)
        {
            ansPiece.clear();
            ansPiece.push_back(i);
            checkExist.clear();
            checkExist.insert(i);
            if(k > 1)
                combine(ans,ansPiece,k,n,checkExist);
        }
        return ans;
    }
    void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, int &n, set<int> &checkExist)
    {
        if(ansPiece.size() == k)
        {
            vector<int> temp = ansPiece;
            ans.push_back(temp);
            return;
        }
        for(int i = 1; i <=n; i++)
        {
            if(checkExist.find(i) == checkExist.end())
            {
                ansPiece.push_back(i);
                checkExist.insert(i);
                combine(ans,ansPiece,k,n,checkExist);
                ansPiece.pop_back();
                checkExist.erase(i);
            }
        }
    }
};

超时2:

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> > ans;
        if(n < k || n <= 0 || k <= 0)
            return ans;
        
        vector<int> ansPiece;
        set<int> checkExist; //记录还没有选的元素
        for(int i = 1; i <= n; i++)
        {
            checkExist.insert(i);
        }
        
        combine(ans,ansPiece,k,checkExist);
        
        return ans;
    }
    void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, set<int> &checkExist)
    {
        if(ansPiece.size() == k)
        {
            vector<int> temp = ansPiece;
            ans.push_back(temp);
            return;
        }
        set<int>::iterator itr;
        for(itr = checkExist.begin(); itr != checkExist.end(); itr++)
        {
            ansPiece.push_back(*itr);
            set<int> check2 = checkExist;
            check2.erase(*itr);
            combine(ans,ansPiece,k,check2);
            ansPiece.pop_back();
        }
    }
};

正确的:(控制了顺序)

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int> > ans;
        if(n < k || n <= 0 || k <= 0)
            return ans;
        
        vector<int> ansPiece;
        int start = 1;
        combine(ans,ansPiece, k,n, start);
        
        return ans;
    }
    void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, int &n, int start)
    {
        if(ansPiece.size() == k)
        {
            vector<int> temp = ansPiece;
            ans.push_back(temp);
            return;
        }
        
        for(int i = start; i <= n; i++)
        {
            if(n - i + 1 < k - ansPiece.size())
                return;
            ansPiece.push_back(i);

            combine(ans,ansPiece,k,n,i+1);

            ansPiece.pop_back();
        }
    }
};

 

LeetCode OJ-- 二战 Combinations

标签:style   blog   color   io   ar   for   art   div   sp   

原文地址:http://www.cnblogs.com/qingcheng/p/3959843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!