[Leetcode] [Java] Next permutation

Source: Internet
Author: User

Title:

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If Such arrangement is not a possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must is in-place, do not allocate extra memory.

Here is some examples. Inputs is in the left-hand column and its corresponding outputs is in the right-hand column.

1,2,3→1,3,2
3,2,1→1,2,3
1,1,5→1,5,1

Test Instructions:

Did not read the question at the beginning, and then finally found a description of the white explanation.

* First to understand: what is next permutation. Simply put, if the number you enter is size, then next permutation is the next one larger than it and "closest" to its full alignment.
* For example, 123, the next use of these three numbers is 132 (213, 312 Although the whole arrangement, but all is greater than 132)
* If the full arrangement is already the largest (in all ascending order), then its next permutation will come up with a "tail to back" (the smallest number):
* For example, the 321 arrangement is already the largest, so next permutation 321 is completely inverted to the smallest 123.

Algorithm Analysis:

Reference: http://harrifeng.github.io/algo/leetcode/next-permutation.html

< Span style= "FONT-FAMILY:SIMSUN; FONT-SIZE:12PX "> Implementation generally divided into the following four steps (corresponding diagram):

    1. From back to front, find the first array[i] < array[i+1] Place, this place is the entire string of the latest "ascending" place, and at the end of the digital, change the words most easily "closest". By comparing from backward to forward, We found that the number 8 was a dividing line. 8 The number behind is descending (that is, the largest)
    2. If we exchange the 6 and 81 directly, it will certainly produce a permutation, but is not "the closest", how to find the "closest", from 1 to 8 behind this end looking for a bigger than 6, and 6 is the smallest difference (that is, from the back to the first 6 larger than the number), we found is 7
    3. 7 as the best choice, and 6 for Exchange. 7 The following must be the decrease (i.e. the maximum value that can be composed)
    4. In order to achieve the "closest", we have grown a lot in the case of 6 becoming 7. So we're going to reverse the array of decrease (which is the smallest value that can be made) after 7.

AC Code:

<span style= "FONT-SIZE:12PX;" >public class Solution {public static void Nextpermutation (int[] num) {if (num.length <= 1) re                Turn for (int i = num.length-2; I >= 0; i--) {if (Num[i] < num[i+1]) {i                NT J; for (j = num.length-1; J >= i; j--) {if (Num[i] < NUM[J])//found larger than num[i] but with the smallest gap                a break;            }//swap the numbres int temp = num[i];            Num[i] = Num[j];            NUM[J] = temp;            Sort the rest of arrays after the swap point arrays.sort (num, i+1, num.length);            Return    }}//If the whole array is an all in descending order (can ' t become bigger),//And you have to reverse the array        for (int i = 0; i < NUM.LENGTH/2; i++) {int temp = Num[i];        Num[i] = num[num.length-i-1]; Num[Num.length-i-1] = temp; } return;}} </span>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Next permutation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.