rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

46. 全排列 #61

Open Ni-Guvara opened 1 year ago

Ni-Guvara commented 1 year ago
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    int rec[21] = {0};
    vector<vector<int>> permute(vector<int>& nums) {
        int len = nums.size();
        backtracing(nums , len);
        return ans;

    }

    void backtracing(vector<int> & nums , int len)
    {
        if(path.size() >= len)
        {
            ans.push_back(path);
            return;
        }

        for(int i = 0 ; i < len ; i++)
        {
            if(rec[nums[i]+10])
                continue;
            path.push_back(nums[i]);
            rec[nums[i] + 10] = 1;
            backtracing(nums  , len);
            path.pop_back();
            rec[nums[i] + 10] = 0;
        }
    }  
};