【一天一道LeetCode】#31. Next Permutation

一天一道LeetCode系列

(一)题目

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)首先从右到左找到第一个破坏升序的值a,

(2)然后再从右至左找到第一个比a大的数b,

(3)交换a和b,

(4)再把交换后的a的位置之后的数反转。

如1,4,5,3,1

第一步找到4,第二步找到5,交换4,5得到15431,然后反转5以后的数,得到15134即为所求。

*/

class Solution {

public:

    void nextPermutation(vector<int>& nums) {

        int len = nums.size();

        if(len!=1){

            int i = len-2;

            while(i>=0 &&nums[i]>=nums[i+1]) i--;

            int j =len-1;

            while(j>i &&nums[j]<=nums[i]) j--;

            if(i>=0) swap(nums[i],nums[j]);

            reverse(nums.begin()+i+1,nums.end());

        }

    }

};
时间: 2024-11-09 04:46:47

【一天一道LeetCode】#31. Next Permutation的相关文章

[array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition 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

【一天一道LeetCode】#60. Permutation Sequence.

一天一道LeetCode系列 (一)题目 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): 1:"123" 2:"132" ? 3 : "213" 4 :&quo

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

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

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 orde

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

[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 repla

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

题目链接: https://leetcode.com/problems/next-permutation/?tab=Description Problem :寻找给定int数组的下一个全排列(要求:be in-place) 倒序查找到该数组中第一个满足后面的数字大于前面的数字的下标i (当前下标 i 指向 后面的那个比较大的数) 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 下一个全排列 */ public class S

LeetCode 31 Next Permutation (C,C++,Java,Python)

Problem: 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