Recursive C++ 8ms Solution. How can i improve the speed ?


  • 0
    S
    class Solution {
    public:
    	vector<vector<int>> result;
    	vector<int> r;
    	void helper(int j,int count,int size,vector<int>& nums) {
    		if (count > size||nums.size()-j
    			return;
    		
    		if (count == size) {
    			result.push_back(r);
    			return;
    		}
    		for (int i = j; i < nums.size() ; i++) {
    			r.push_back(nums[i]);
    			helper(i + 1, count + 1, size, nums);
    			r.pop_back();
    		}
    	}
    	vector<vector<int>> subsets(vector<int>& nums) {
    		vector<int> r;
    		for (int j = 0; j < nums.size(); j++) {
    			helper(0,0,j+1, nums);
    		}
    		result.push_back(vector<int>{});
    		return result;
    	}
    };
    

Log in to reply
 

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