31. Next Permutation

原创 2016年08月31日 10:41:31

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

public class Solution {
    public void nextPermutation(int[] A) {
        if(A == null || A.length <= 1) return;
        int i = A.length - 2;
        while(i >= 0 && A[i] >= A[i + 1]) i--; // Find 1st id i that breaks descending order
        if(i >= 0) {                           // If not entirely descending
            int j = A.length - 1;              // Start from the end
            while(A[j] <= A[i]) j--;           // Find rightmost first larger id j
            swap(A, i, j);                     // Switch i and j
        }
        reverse(A, i + 1, A.length - 1);       // Reverse the descending sequence
    }

    public void swap(int[] A, int i, int j) { // 俩交换方法差不多
        // int tmp = A[i];
        // A[i] = A[j];
        // A[j] = tmp;
        A[i] = A[i] + A[j];
        A[j] = A[i] - A[j];
        A[i] = A[i] - A[j];
    }

    public void reverse(int[] A, int i, int j) {
        while(i < j) swap(A, i++, j--);
    }
}
// 题目思路
// 先右边找第一个违反递减规则的数,这个数后面就是递减的了,该数为目标数a
// 再从右边找第一个比目标数a大的数,该数为目标数b
// a b互换,中间的递减序列分别互换,得结果
// 思路相当棒啊~~~~
版权声明:本文为博主原创文章,转载须注明出处,谢谢~ 举报

相关文章推荐

31. Next Permutation

本文摘自 LeetCode 题解3 1. Next PermutationImplement next permutation, which rearranges numbers into the l...

31. Next Permutation

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

LeetCode 31 Next Permutation

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

LeetCode --- 31. Next Permutation

题目链接:Next Permutation Implement next permutation, which rearranges numbers into the lexicographical...

LEETCODE 31. Next Permutation

题目大意给出一个数组nums,求出nums的下一个permutation。 比如{1,2,3,4}\{1,2,3,4\},他的下一个permutation是{1,2,4,3}\{1,2,4,3\} ...

(Leetcode)31. Next Permutation

Problem 31. Next Permutation: Implement next permutation, which rearranges numbers into the lex...

Leetcode——31. Next Permutation

题目Implement next permutation, which rearranges numbers into the lexicographically next greater permu...

[LeetCode]31. Next Permutation

31. Next Permutation Implement next permutation, which rearranges numbers into the lexicographical...

LeetCode 31. Next Permutation

题目: Implement next permutation, which rearranges numbers into the lexicographically next greate...

LeetCode(31)--Next Permutation

就是将这个数组排列成下一个较大的数,如果已经是最大的了,就排列成最小的数。 我是这么想的,从后往前遍历数组,如果一直是逐渐增大的,则已经是最大的了,如果出现了一个下降的数,那么遍历就到此为止,因为这...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)