My easy c++ solution


  • 2
    V
    vector<vector<int>> res;
    vector<vector<int>> combine(int n, int k) {
    	vector<int> nums(n);
    	vector<int> temp;
    	for (int i = 0; i < n; i++) {
    		nums[i] = i + 1;
    	}
    	helper(nums, k, temp);
    	return res;
    }
    
    void helper(vector<int> nums, int k, vector<int> temp) {
    	if (k == 0) {
    		res.push_back(temp);
    		return;
    	}
    	if (nums.size() < k)
    		return;
    	for (int i = 0; i < nums.size(); i++) {
    		vector<int> n = nums;
    		vector<int> tp = temp;
    		tp.push_back(nums[i]);
    		vector<int>::iterator it = n.begin() + i + 1;
    		n.erase(n.begin(),it);
    		helper(n, k-1, tp);
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.