C++ AC solution using backtracking


  • 0
    W
    class Solution {
    public:
        vector<vector<int>> permute(vector<int>& nums) {
            vector<vector<int>> res;
            backtrack(res, vector<int>(), nums);
            return res;
        }
        
        void backtrack(vector<vector<int>> &res, vector<int> temp, vector<int> &nums){
            if(temp.size()==nums.size()){
                res.push_back(temp);
                return;
            }
            for(int i : nums){
                if(find(temp.begin(), temp.end(), i) == temp.end()){
                    vector<int> temp2(temp);
                    temp2.push_back(i);
                    backtrack(res, temp2, nums);
                }
            }
            return;
        }
    };
    

Log in to reply
 

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