My easy understanding c++ code


  • 0
    H
    class Solution {
    public:
    	void dfs(vector<vector<int>> &res, vector<int> nums, vector<int> cur){
    		for (int i = 0; i < nums.size(); i++){
    			cur.push_back(nums[i]);
    			vector<int> tmp = nums;
    			tmp.erase(tmp.begin() + i);
    			if (tmp.size() == 0){
    				res.push_back(cur);
    				return;
    			}
    			dfs(res, tmp, cur);
    			cur.pop_back();
    		}
    	}
    	vector<vector<int>> permute(vector<int>& nums) {
    		vector<vector<int>> res;
    		vector<int> cur;
    		dfs(res, nums, cur);
    		return res;
    	}
    };
    

Log in to reply
 

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