无聊啊 Next Permutation

不甚知道意义的题目,不过数组reverse值得复习一下哟

public class Solution {
    public void nextPermutation(int[] num) {
        //这个题目意义何在啊唉

        if (num==null || num.length==0) return;

        // 先找到第一个不符合从右往左一次变大的数 X
        int i=num.length-2;
        while(i>=0 && num[i]>=num[i+1]){
            i--;
        }

        // 从右开始找到第一个比X大的数num[j]
        if(i>=0){
        int j = num.length-1;
        while(j>i && num[j]<=num[i]){
            j--;
        }
        swap(num, i, j);
        }

        reverse(num, i+1, num.length-1);
    }

    public void swap(int[] num, int i, int j){
        int t = num[i];
        num[i]= num[j];
        num[j] = t;
    }
    public void reverse(int[] num, int st, int end){
        while(st<end){
            swap(num, st, end);
            st++;
            end--;
        }
    }
}
时间: 2024-08-24 22:06:16

无聊啊 Next Permutation的相关文章

BNU 51636 Squared Permutation 线段树

Squared Permutation 最近,无聊的过河船同学在玩一种奇怪的名为“小Q的恶作剧”的纸牌游戏. 现在过河船同学手有张牌,分别写着,打乱顺序之后排成一行,位置从左往右按照标号. 接下来小Q同学会给出个操作,分为以下两种: 1.给定,交换从左往右数的第和第张牌, 2.给定,对从左往右数的第张牌,记下位置是这张牌上的数字的牌的数字,询问所有记下的数字加起来的结果. 虽然无聊的过河船同学精通四则运算,但是要完成这么大的计算量还是太辛苦了,希望你能帮他处理这些操作. Input 第一行是一个

无聊的链表

无聊写了下无头结点的链表,发现没有头结点想做一些插入/删除操作果然很困难... #include <stdio.h> #include <stdlib.h> typedef struct NotHeadLinkList { int data; struct NotHeadLinkList * next; }NotHeadLinkList; NotHeadLinkList * h, * t, * s = NULL; // 头指针.尾指针 int main() { int i , n

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

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): "123" "132" "213" "231" "312" "3

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

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): "123" "132" "213" "231" "312" "3

【数组】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 repla

[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