leetcode_31_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 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

分析:

详细内容可参考STL源码剖析中关于next_permutation分析部分,下图为简单示意图,可辅助理解

简单分析——先验知识

1、reverse_iterator测试

#include <iostream>

#include <stdio.h>

#include <vector>

using namespace std;

int main()

{

vector<int> A({1, 2, 3, 4});

for(int i=0; i<A.size(); i++)

cout << A[i];

cout << endl;

auto rfirst = vector<int>::reverse_iterator(A.end());

auto rlast = vector<int>::reverse_iterator(A.begin());

cout << *rfirst << endl;

cout << *rlast;

}

2、查找元素值大于10的元素的个数

int res
= count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));

3、查找第一个比
3 大的数值 , 下面的代码的意思是找到第一个 3 比取出的数值大的数的位置。bind1st 表示要绑定一个函数,绑定一个函数, greater<int>(),3 ,表示比三大的数。

auto ifind
= find_if(mylist.begin(), mylist.end (), bind1st( greater<int>(), 3));

//方法一:调用next_permutation函数
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        next_permutation(nums.begin(), nums.end());
    }
};
//方法二:自己写一个next_permutation函数
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        next_permutationUtil(nums.begin(), nums.end());
    }

    template <typename T>
    bool next_permutationUtil(T first, T last) //如果要输出全排列,则首先要排序为递增序列
    {
        auto rfirst = reverse_iterator<T>(last);
        auto rlast = reverse_iterator<T>(first);
        auto point = next(rfirst);

        while(point != rlast && *point >= *prev(point)) //存在递增序列对
            point++;
        if(point == rlast) //结束标志,整个序列已经是递减序列
        {
            reverse(rfirst, rlast);
            return false;
        }
        auto change = find_if(rfirst, point, bind1st(less<int>(), *point)); //找到第一个比*point大的值
        swap(*point, *change);
        reverse(rfirst, point); //The range used is [first,last)
        return true;
    }
};
时间: 2024-08-19 16:01:53

leetcode_31_Next Permutation的相关文章

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

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): "123" "132" "213" "231" "312" "3

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

Problem: https://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 poss

60. 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): "123" "132" "213" "231" "312" "3

【数组】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]题解(python):031-Next Permutation

题目来源 https://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

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l