Leetcode_next-permutation(c++ and python version)

62 篇文章 0 订阅
52 篇文章 0 订阅

地址:http://oj.leetcode.com/problems/next-permutation/

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

思路:在讲思路前插个曲。晚上听了大部分worldquant的兼职招聘的宣讲后提前退场回实验室刷题了,但是发现Leetcode挂了,还挂了很久,至少一个半小时,虽然之前遇到好几次leetcode挂了的现象,但是基本几分钟十几分钟就好了。所以无意间发现了这个:

http://www.keithschwarz.com/interesting/code/?dir=next-permutation

正好是此题。

大意是:

1. 从数组尾向头找到最大连续升序的序列 x

2. 将序列x起点(数组尾为终点)左边的数(如果有的话),记为y,与该序列中大于y的最小的数交换

3. 将交换后的序列x排序(从小到大)

c++参考代码:

class Solution {
public:
    void nextPermutation(vector<int> &num) {
        if(num.empty())
            return;
        int pos = num.size()-1;
        while(pos && num[pos]<=num[pos-1])
            --pos;
        if(!pos)
            sort(num.begin(), num.end());
        else
        {
            for(int i = num.size()-1; i>=pos; --i)
                if(num[i] > num[pos-1])
                {
                    swap(num[i], num[pos-1]);
                    break;
                }
            sort(num.begin()+pos, num.end());
        }
    }
};

python参考代码:

class Solution:
    # @param num, a list of integer
    # @return a list of integer
    def nextPermutation(self, num):
        if not num:
            return num
        for i in range(len(num)-1,0,-1):
            if num[i] > num[i-1]:
                for j in range(len(num)-1,i-1,-1):
                    if num[j] > num[i-1]:
                        num[i-1], num[j] = num[j], num[i-1]
                        break
                return num[:i] + sorted(num[i:])
        return sorted(num)

 
 
//what's more, it can also abandon sorting at last. What's amazing is the code below is 20ms+ later than the upper one.
//I think there must be some optimizations in STL.
//coding two weeks later.

class Solution {
public :
     void nextPermutation ( vector < int > & num ) {
         if ( num . empty () || num . size () == 1 )
             return ;
         int pos = num . size () - 1 ;
         for (; pos >= 1 ; -- pos )
         {
             if ( num [ pos ] > num [ pos - 1 ])
                 break ;
         }
         if ( ! pos )
         {
             for ( int i = 0 , j = num . size () - 1 ; i < j ; ++ i , -- j )
                 swap ( num [ i ], num [ j ]);
         }
         else
         {
             for ( int i = num . size () - 1 ; i >= pos ; -- i )
             {
                 if ( num [ i ] > num [ pos - 1 ])
                 {
                     swap ( num [ i ], num [ pos - 1 ]);
                     break ;
                 }
             }
             for ( int j = num . size () - 1 ; j > pos ; -- j , ++ pos )
                 swap ( num [ j ], num [ pos ]);
             return ;
         }
     }
};


 
 
//Thrid trial
class Solution {
public :
     void nextPermutation ( vector < int > & num ) {
         if ( num . empty ())
             return ;
         int sz = num . size (), pos = 0 ;
         for ( pos = sz - 1 ; pos >= 1 ; -- pos )
         {
             if ( num [ pos ] > num [ pos - 1 ])
                 break ;
         }
         if ( ! pos )
             reverse ( num . begin (), num . end ());
         else
         {
             for ( int i = sz - 1 ; i >= pos ; -- i )
             {
                 if ( num [ i ] > num [ pos - 1 ])
                 {
                     swap ( num [ i ], num [ pos - 1 ]);
                     break ;
                 }
             }
             reverse ( num . begin () + pos , num . end ());
         }
     }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值