Neat c++ solution


  • 4
    I
    void help(vector<vector<int>>& res, vector<int>& cur, int start) {
        if (start == cur.size()) {
            res.push_back(cur);
        } else {
            for (int i = start; i < cur.size(); i++) {
                swap(cur[start], cur[i]);
                help(res, cur, start + 1);
                swap(cur[start], cur[i]);
            }
        }
    }
    
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        help(res, nums, 0);
        return res;
    }

Log in to reply
 

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