码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode: Next Permutation

时间:2015-04-20 12:52:32      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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--;
                }
            }
    }
};

 

LeetCode: Next Permutation

标签:

原文地址:http://www.cnblogs.com/yxzfscg/p/4440932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!