[leetcode 52] Next Permutation Report

Question:

Given a list of integers, which denote a permutation.

Find the next permutation in ascending order.

Example

For [1,3,2,3], the next permutation is [1,3,3,2]

For [4,3,2,1], the next permutation is [1,2,3,4]

Note

The list may contains duplicate integers.

What is Next Permutation?

The permutation with current charactors that is lexicographically next.

1234 -> 1243
4321 -> 1234
1243 -> 1324
2134 -> 2143

Idea:

Answer:

 class Solution {
 public:
     /**
      * @param nums: An array of integers
      * @return: An array of integers that‘s next permuation
      */
     vector<int> nextPermutation(vector<int> &nums) {
         // write your code here
         if (nums.size() <= 1) {
             return nums;
         }

         //find first element that is smaller that next element
         int first_small_index = -1;
         for (int i = nums.size() - 2; i >= 0; --i) {
             if (nums[i] < nums[i+1]) {
                 first_small_index = i;
                 break;
             }
         }
         //find the first element that is just bigger than the element we found
         if (first_small_index != -1) {
             int first_bigger_index = first_small_index;
             for (int i = first_small_index + 1; i < nums.size(); ++i) {
                 if (nums[i] > nums[first_small_index]) {
                     first_bigger_index = i;
                 } else {
                     break;
                 }
             }
             swap(nums[first_small_index], nums[first_bigger_index]);
         }
         // Reverse rest array
         reverse(nums.begin() + first_small_index + 1, nums.end());

         return nums;
     }
 };
时间: 2024-09-29 02:03:21

[leetcode 52] Next Permutation Report的相关文章

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

[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] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

[Lintcode]52. Next Permutation

52. Next Permutation 本题难度: Medium Topic: Greedy Description 52. Next Permutation 本题难度: Medium Topic: Greedy Description Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Example Example 1: Input:[1] O

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 (下一个排列)

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][JavaScript]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