• LeetCode: Next Permutation & Permutations1,2


    Title:

    Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

    If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

    The replacement must be in-place, do not allocate extra memory.

    Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
    1,2,3 → 1,3,2
    3,2,1 → 1,2,3
    1,1,5 → 1,5,1

    思路:

    联想实际操作,一般都是从最后一位开始分析。就会发现实际上我们需要找的就是从末尾开始,一直保持逆序的位置。比如1,4,3,2中后面的4,3,2都是逆序,因此需要替换1,找到1后面的稍微比它大的数。大致思路就是这样。

    class Solution {
    public:
        void nextPermutation(vector<int> &num) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            int nSize = num.size();
            if (nSize <= 1) return;
            
            int idx = nSize - 1;
            // 查找第一个下降的元素
            while(--idx >= 0 && num[idx] >= num[idx+1]);
            if (idx >= 0)
            {
                int i = nSize - 1;
                // 查找第一个比idx所指元素大的元素
                while(num[i] <= num[idx])
                {
                    --i;
                }
                swap(num[i], num[idx]);
                // 反转后面所有元素,让它从小到大sorted
                reverse(num.begin()+idx+1, num.end());
            }
            else
            {
                reverse(num.begin(), num.end());
            }
        }
    };

    查找可用二分查找,虽然最后的时间都差不多

    class Solution {
    public:
        void swap(int &i, int& j){
            i = i+j;
            j=i-j;
            i=i-j;
        }
    
    
        int find(vector<int> &num, int startIndex, int v){
            int l = startIndex;
            int h = num.size()-1;
            while (l <= h){
                int m = (l+h)/2;
                if (num[m] > v){
                    l = m+1;
                }else{
                    h = m-1;
                }
            }
            return h;
        }
        void nextPermutation(vector<int> &num) {
            if (num.size() <= 1)
                return ;
            int index = num.size()-1;
            int min;
            int l,r;
                while ( index > 0 && num[index-1] >= num[index]){
                    index--;
                }
                //cout<<index<<endl;
                if (index == 0){
                    //到头
                    l = 0;
                    r = num.size()-1;
                    while (l < r){
                        swap(num[l],num[r]);
                        l++;
                        r--;
                    }
                }else{
                    //从后面的序列中找到比当前的数刚好大得那个数,并交换,然后重新从小到大排序
                    int temp = find(num,index,num[index-1]);
                    //cout<<"temp: "<<temp<<endl;
                    swap(num[index-1],num[temp]);
                    //sort(num.begin()+index,num.end());
                    l = index;
                    r = num.size()-1;
                    while (l < r){
                        swap(num[l],num[r]);
                        l++;
                        r--;
                    }
                }
        }
    };

    Title:

    Given a collection of numbers that might contain duplicates, return all possible unique permutations.

    For example,
    [1,1,2] have the following unique permutations:
    [1,1,2][1,2,1], and [2,1,1].

    思路,DFS搜索

    void internalpermuteUnique(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
            int size = num.size();
            
            if (size == index) {
                result.push_back(perm);
            }
            else {
                for (int i = index; i < size; ++i) {
                    if ((i > index) && (num[i] == num[index])) {
                      continue;
                    }
                    else {
                      swap(num[index], num[i]);
                    }
                    
                    perm.push_back(num[index]);
                    internalpermuteUnique(num, index + 1, perm, result);
                    perm.pop_back();
                    swap(num[index], num[i]);
                }
               //sort(num.begin() + index, num.end());
            }
        }
        
        vector<vector<int> > permuteUnique(vector<int> &num) {
            vector<vector<int> > result;
            vector<int> perm;
            
            sort(num.begin(), num.end());
            internalpermuteUnique(num, 0, perm, result);
            
            return result;
        }


    Title: Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations.

    
    

    By listing and labeling all of the permutations in order,
    We get the following sequence (ie, for n = 3):

    
    
    1. "123"
    2. "132"
    3. "213"
    4. "231"
    5. "312"
    6. "321"
    
    
    
    
    

    Given n and k, return the kth permutation sequence.

    思路:题目不难,通过演算就可以找到规律,不过要注意下标,是从0开始的。所以最开始要将k--

     
    class Solution{
    public:
        string getPermutation(int n,int k){
            vector<int> v;
            string result;
            if (k <= 0)
                return result;
            if ( n < 0 || n > 9)
                return result;
            for (int i = 1; i <= n; i++)
                v.push_back(i);
            k--;
            while (n > 1){
                int c = getFactorial(--n);
                int a = k / c;
                int b = k % c;
                result.push_back(v[a]+'0');
                v.erase(v.begin()+a);
                k = b;
            }
            result.push_back(v[0]+'0');
            return result;
        }
        int getFactorial(int k){
            if (k == 1)
                return 1;
            else 
                return k * getFactorial(k-1);
        }
    };
    
    
    
     
  • 相关阅读:
    尚硅谷JavaWeb篇
    malloc 的线程安全与不可重入性
    MVCC 的一些总结
    LINUX网络子系统中DMA机制的实现
    JDK动态代理的实现原理
    Linux Polkit 权限提升漏洞预警(CVE20214034)
    [译]深入了解现代web浏览器(一)
    go语言使用redis实现异步任务
    Python 金额保留两位小数
    Django admin filter_horizontal 将多对多字段左右编辑
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4440932.html
Copyright © 2020-2023  润新知