leetcode 排列

https://leetcode-cn.com/problems/next-permutation/(下一个排列)

class Solution {
public:
    void nextPermutation(vector &nums) {
        int i = (int)nums.size() - 2;
        while (i >= 0 && nums[i + 1] <= nums[i]) {
            i--;
        }
        if (i >= 0) {
            int j = (int)nums.size() - 1;
            while (j >= 0 && nums[j] <= nums[i]) {
                j--;
            }
            swap(nums[i], nums[j]);
        }
        reverse(nums, i + 1);
    }

    void reverse(vector &nums, int start) {
        int i = start, j = (int)nums.size() - 1;
        while (i < j) {
            swap(nums[i], nums[j]);
            i++;
            j--;
        }
    }
};

https://leetcode-cn.com/problems/permutations/(全排列,无重复元素)

class Solution {
public:
    vector> permute(vector& nums) {
        vector> ans;
        vector track;
        backtrack(nums, track, ans);
        return ans;
    }
    
    void backtrack(vector& nums, vector &track, vector> &ans)
    {
        if (track.size() == nums.size())
        {
            ans.push_back(track);
        }
        else
        {
            for (int i = 0; i < nums.size(); ++i)
            {
                if (find(track.begin(), track.end(), nums[i]) != track.end())
                    continue;
                
                track.push_back(nums[i]);
                backtrack(nums,track,ans);
                track.pop_back();
            }
        }
    }
};

https://leetcode-cn.com/problems/permutations-ii/ (全排列,有重复元素)

class Solution {
public:
    vector> permuteUnique(vector& nums) {
        vector> ans;
        vector track;
        vector visited(nums.size(), false);
        sort(nums.begin(), nums.end());
        backtrack(nums, track, visited, ans);
        return ans;
    }
    
    void backtrack(vector& nums, vector &track, vector &visited, vector> &ans)
    {
        if (track.size() == nums.size())
        {
            ans.push_back(track);
        }
        else
        {
            for (int i = 0; i < nums.size(); ++i)
            {
                if (visited[i]) continue;
                
                if (i != 0 && nums[i] == nums[i-1] && !visited[i-1]) continue;
                    
                visited[i] = true;
                track.push_back(nums[i]);
                
                backtrack(nums,track,visited,ans);
                
                track.pop_back();
                visited[i] = false;
            }
        }
    }
};

60. 第k个排列

class Solution {
public:
    string getPermutation(int n, int k) {
        list nums;
        int factorial = 1;
        for (int i = 1; i <= n; ++i)
        {
            nums.push_back(i);
            factorial *= i;
        }
        
        k = k - 1;
        stringstream ss;
        for (int i = n; i > 0; --i)
        {
            factorial /= i;
            ss << getNth(nums, k/factorial);
            k %= factorial;
        }
        
        return ss.str();
    }
    
    int getNth(list &nums, int n)
    {
        auto it = nums.begin();
        for (int i = 0; i < n; ++i)
        {
            ++it;
        }
        
        int num = *it;
        nums.erase(it);
        return num;
    }
};

 

你可能感兴趣的:(leetcode)