LeetCode-Next Permutation-下一个排列-扫描+排序(不是最优解)

https://oj.leetcode.com/problems/next-permutation/

我这个解法并不很好,是O(n^2),这个在STL实现里应该是O(n)的。不过我的方法比较简单。

想法是首先需要找到一个left边界l,这个边界需要跟其后面的某个元素r交换使序列增大。然后在l前面的部分不变。后面的部分尽可能的小。

这个与l交换的r需要满足如下两个条件:

1)a[r]>a[l],交换必须使得序列增大。

2)在[l+1,n)里,满足a[i]>a[l]的所有i里面a[r]应该是最小的。

在这样交换后,对[l+1,n)进行一次排序,使其序列最小。即可。

const int INF=9999;
class Solution {
public:
    int n,m;
    void nextPermutation(vector<int> &num) {
        n=num.size();
        if (n<2) return;
        int l,r;
        for (l=n-2;l>=0;l--){
            int rv=INF;
            r=-1;
            for (int i=l+1;i<n;i++){
                if (num[i]>num[l] && num[i]<=rv){
                    rv=num[i];
                    r=i;
                }
            }
            if (r!=-1) {
                break;
            }
        }
        if (r!=-1) {
            swap(num[l],num[r]);
            sort(num.begin()+l+1,num.end());
        }
        else sort(num.begin(),num.end());
    }
};

  

时间: 2024-08-09 03:15:10

LeetCode-Next Permutation-下一个排列-扫描+排序(不是最优解)的相关文章

Leetcode:Next Permutation 下一个排列

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 ord

【LeetCode每天一题】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 replaceme

lintcode 中等题:next permutation下一个排列

题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] 注意 排列中可能包含重复的整数 解题 和上一题求上一个排列应该很类似 1.对这个数,先从右到左找到递增序列的前一个位置,peakInd 2.若peakInd = -1 这个数直接逆序就是答案了 3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1

[LeetCode]31. 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 replaceme

[leetcode]31. 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 replaceme

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically

[C++]LeetCode: 79 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 repla

LeetCode 31. 下一个排列(Next Permutation)

题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. 以下是一些例子,输入位于左侧列,其相应输出位于右侧列. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 解题思路 由于各个排列按照字典序排序,所以以 1,3,2 → 2,1,3为例,寻找下一个排列的步骤是: 首先找到从后往前第一个升序数对,在此例中即(1

stl 之 next_permutation 求出一个排序的下一个排列的函数 转载

这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类型的next_permutation int main(){ int a[3];a[0]=1;a[1]=2;a[2]=3; do{cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<