My C++ Code (16ms)


  • 0

    I just used the find function to solve this problem. Because there is no find function when we submit our code, so I just write a find function.

    #code block

    class Solution {
    public:
    vector<vector<int>> permute(vector<int>& nums) {
    vector<vector<int>> res;
    vector<int> path;
    find(nums,path,res,0);
    return res;
    }

    void find(vector<int>& nums, vector<int>& path, vector<vector<int>>& res, int begin){
        if (nums.size() <= path.size()){res.push_back(path); return;}
        
        for (int i = begin;i < nums.size(); i++) {
            if (find(path.begin(), path.end(),nums[i]) ==  path.end()) {
                path.push_back(nums[i]);
                find(nums,path,res,begin);
                path.pop_back();
            }
        }
    }
    

    template<class InputIterator, class T>
    InputIterator find (InputIterator first, InputIterator last, const T& val)
    {
    while (first!=last) {
    if (*first==val) return first;
    ++first;
    }
    return last;
    }
    };


Log in to reply
 

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