Next Permutation[leetcode]

基本思路是从后往前找到第一个递减的数num[index],并与之前的第一个大于num[index]的数交换位置。

注意交换后[index+1...n-1]仍是非递减的,所以只需要reverse一下,使其变成非递增的

    void nextPermutation(vector<int> &num) {
        int index = num.size() - 2;
        int rIndex = index;
        //traverse forward till find first decending num, save in index
        for (; index >= 0; index--)
        {
            //find first num larger than nun[index], save in rIndex
            if (num[index + 1] > num[index])
            {
                for (rIndex = num.size() - 1; rIndex > index && num[index] >= num[rIndex]; rIndex--);
                swap(*(num.begin() + index), *(num.begin() + rIndex));
                break;
            }
        }
        reverse(num.begin() + index + 1, num.end());//do not need sort, just reverse
    }
时间: 2024-10-30 03:35:05

Next Permutation[leetcode]的相关文章

Palindrome Permutation -- LeetCode

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. 要点:学习如何iterate一个unordered_map. 1 class Solution { 2 public: 3 bool canP

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

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]题解(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

Leetcode Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

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: Permutation Sequcence 题解

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

Leetcode permutation 系列

关于permutation的讲解,请参见http://blog.csdn.net/xuqingict/article/details/24840183 下列题目的讲解均是基于上面的文章: 题1: Next Permutation Total Accepted: 8066 Total Submissions: 32493My Submissions Implement next permutation, which rearranges numbers into the lexicographic

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s