《下一个排列: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 and use only constant 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

解题思路:

permutation是排列的意思,就是几个数字所有可能的排列。比如0,1,2这三个数,它们的permutation就是012 021 102 120 201 210。lexicographeically greater就是大小排序,012 < 021 < 102 < 120 < 201 < 210。以1 3 5 4 2 1为例,输出应当为 1 4 1 2 3 5。

解题步骤:

1 3 5 4 2 1 从右向左搜索,5到3是下降。

1 4 5 3 2 1 将4和3进行交换。

1 4 1 2 3 5 将 5 3 2 1进行升序排列。

代码如下:

#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    void nextPermutation(vector<int>& nums)
    {
        int n = nums.size();
        int i = n - 2;
        int j = n - 1;
        while (i >= 0 && nums.at(i) >= nums.at(i+1))
        {
            --i;
        }
        if (i >= 0)
        {
            while (nums.at(i) < nums.at(j))
            {
                --j;
            }
            swap(nums.at(i), nums.at(j+1));
            reverse(nums.begin() + i + 1, nums.end());
        }
        else
            reverse(nums.begin(), nums.end());

    }
};

int main()
{
    Solution mySolution;
    //case 1
    vector <int> Input1;
    Input1.push_back(1);
    Input1.push_back(2);
    Input1.push_back(3);
    for (int i = 0; i < Input1.size() - 1; i++)
    {
        cout << Input1.at(i) << ",";
    }
    cout << Input1.at(Input1.size() - 1) << "->";
    mySolution.nextPermutation(Input1);
    for (int i = 0; i < Input1.size() - 1; i++)
    {
        cout << Input1.at(i) << ",";
    }
    cout << Input1.at(Input1.size() - 1) << endl;
    //case 2
    vector <int> Input2;
    Input2.push_back(3);
    Input2.push_back(2);
    Input2.push_back(1);
    for (int i = 0; i < Input2.size() - 1; i++)
    {
        cout << Input2.at(i) << ",";
    }
    cout << Input2.at(Input2.size() - 1) << "->";
    mySolution.nextPermutation(Input2);
    for (int i = 0; i < Input2.size() - 1; i++)
    {
        cout << Input2.at(i) << ",";
    }
    cout << Input2.at(Input2.size() - 1) << endl;
    //case 3
    vector <int> Input3;
    Input3.push_back(1);
    Input3.push_back(1);
    Input3.push_back(5);
    for (int i = 0; i < Input3.size() - 1; i++)
    {
        cout << Input3.at(i) << ",";
    }
    cout << Input3.at(Input3.size() - 1) << "->";
    mySolution.nextPermutation(Input3);
    for (int i = 0; i < Input3.size() - 1; i++)
    {
        cout << Input3.at(i) << ",";
    }
    cout << Input3.at(Input3.size() - 1) << endl;
    system("pause");
    return 0;
}

运行结果截图:

《下一个排列:Next Permutation》

往期回顾

《两个排序数列的中位数:Median of Two Sorted Arrays》

《最长无重复字符: Longest Substring Without Repeating Characters》

《两数字相加:Add Two Numbers》

《两数和:Two Sum》

《最长回文子串:Longest Palindromic Substring》

《正则表达式匹配:Regular Expression Matching》

《装最多水的容器:Container With Most Water》

《三数之和:3Sum》

《电话号码的字母组合:Letter Combinations of a Phone Number》

《移除链表倒数第N个节点:Remove Nth Node From End of List》

《验证括号:Valid Parentheses》

《混合插入有序链表: Merge Two Sorted Lists》

《生成括号: Generate Parentheses》

《合并k个有序链表:Merge k Sorted Lists 》