4ms c++ solution using recursion


  • 9
    A
    class Solution {
    public:
    	void f(vector<int>& temp,vector<vector<int>>&result,int i,vector<int>& nums){
    		if(i==nums.size()){
    			result.push_back(temp);
    			return;
    		}
    		temp.push_back(nums[i]);
    		f(temp,result,i+1,nums);
    		temp.pop_back();
    		f(temp,result,i+1,nums);
    		return;
    	}
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            vector<int> temp;
    		vector<vector<int>>result;
    		f(temp,result,0,nums);
    		return result;
        }
    };

  • 0
    A

    How did you formulate above recursion? Please Explain


  • 0
    A

    This solution gives 8ms running time, not 4ms!


  • 1

    it is fantastic. How did you come up with this?


Log in to reply
 

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