组合问题

给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @param k int整型 
     * @return int整型vector<vector<>>
     */
    void backTracking(int n,int deepth,int k,vector<int> path,vector<vector<int> >& v) 
    {
    	int i;
    	if(path.empty())
    	{
    		i=1;
		}
		else
		{
			i=path.back()+1;
		}
    	for(;i<=n;++i)
    	{
    		path.push_back(i);
    		if(deepth==k)
    		{
    			v.push_back(path);
    			path.pop_back();
			}
			else
			{
				backTracking(n,deepth+1,k,path,v);
				path.pop_back();
			}
		}
	}
    vector<vector<int> > combine(int n, int k) 
    {
        // write code here
        if(n==0)
        {
        	return vector<vector<int>>();
		}
        else
        {
            vector<vector<int> > v;
       	    vector<int> path;
       	    backTracking(n,1,k,path,v);
       	    return v;
        }
        
        
    }
};

};

全部评论
赞,对我学习很有帮助
1
送花
回复
分享
发布于 2023-03-04 17:25 重庆
又是让我好好学习的一天
点赞
送花
回复
分享
发布于 2023-04-03 11:57 湖南
秋招专场
校招火热招聘中
官网直投

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务