美文网首页
combinations

combinations

作者: DaiMorph | 来源:发表于2019-06-28 00:55 被阅读0次
    class Solution {
    public:
        vector<vector<int> > combine(int n, int k) {
            vector<vector<int>>ans;
            vector<int>path;
            dfs(1,0,n,k,path,ans);
            return ans;
        }
        void dfs(int cur,int cnt,int n,int k,vector<int>&path,vector<vector<int>>&ans)
        {
            if(cnt==k)
            {
                ans.push_back(path);
                return;
            }
            if(cur>n)return;
            path.push_back(cur);
            dfs(cur+1,cnt+1,n,k,path,ans);
            path.pop_back();
            dfs(cur+1,cnt,n,k,path,ans);
        }
    };
    

    相关文章

      网友评论

          本文标题:combinations

          本文链接:https://www.haomeiwen.com/subject/pvrjcctx.html