LeetCode Next Permutation

原题链接在这里:https://leetcode.com/problems/next-permutation/

参考了这篇博文:http://blog.csdn.net/linhuanmars/article/details/20434115

分两种情况:

1. 从前往后一路数值一直变小,如"654321", 返回的应该是"123456"也就是整个反转

2. 数值没有一直变下,如"236541", 此时从后往前找到第一个数值下降的点3<6, 找到3,记下index i,

然后从3往后开始找第一个比3 小的数, 1<3, 找到1, 记下1前一个点的index j. swap nums[i] and nums[j]. 变成"246531"

最后反转 i 后面的部分, 得到"241356".

Note: 找i的时候是 nums[i] >= nums[i+1] 等于时也往前移动i, 只有小于时才停下。

AC Java:

public class Solution {
    public void nextPermutation(int[] nums) {
        //以2,3,6,5,4,1为例
        //从后往前,找到第一个变小的的数字,如3
        //从3的下一位开始向后找到第一个比3小的数字的前一个数,1比3小,1前面的数是4
        //调换3 和 4,然后反转 调换后 4后面的数字
        if(nums == null || nums.length == 0){
            return;
        }
        int i = nums.length -  2;
        while(i>=0 && nums[i]>=nums[i+1]){
            i--;
        }
        if(i>=0){
            int j = i+1;
            while(j<nums.length && nums[j]>nums[i]){
                j++;
            }
            j--;
            swap(nums, i,j);
        }
        reverse(nums,i+1,nums.length-1);
    }
    private void swap(int [] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    private void reverse(int [] nums, int i, int j){
        while(i<j){
            swap(nums,i++,j--);
        }
    }
}
时间: 2024-11-09 16:24:38

LeetCode Next Permutation的相关文章

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 OJ--Next Permutation *

求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int> &num) { if(num.size() == 0) return; c

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

LeetCode: Next Permutation [030]

[题目] 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 repl

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

LeetCode——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]Next Permutation @ Python

原题地址:https://oj.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 p

Leetcode 60. Permutation Sequence

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): Given n and k, return the kth permutation sequence. Note: Given n will be between