031 Next Permutation

又是一道纯数学的题, 纸上认真研究下就好。 这道题需要Inplace, 所以写了个辅助的reverse.

class Solution:
    # @param {integer[]} nums
    # @return {void} Do not return anything, modify nums in-place instead.
    def nextPermutation(self, nums):
        length = len(nums)
        l = length - 1
        while l >= 1 and nums[l] <= nums[l-1]:
            l -= 1
        if l == 0:
            self.reverse(nums, 0, length - 1)
        else:
            j = length - 1
            while nums[j] <= nums[l-1]:
                j -= 1
            nums[l-1], nums[j] = nums[j], nums[l-1]
            self.reverse(nums, l, length - 1)
        print(nums)

    def reverse(self, nums, i, j):
        while i < j:
            nums[i], nums[j] = nums[j], nums[i]
            i += 1
            j -= 1
时间: 2024-07-31 03:07:00

031 Next Permutation的相关文章

[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 题意: 求一个序列的下一个排列. 分析: 可以用

【LeetCode】031. 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

Java for LeetCode 031 Next Permutation

Next Permutation Total Accepted: 33595 Total Submissions: 134095 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 low

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

AtCoder Grand Contest 031 (AGC031) F - Permutation and Minimum 动态规划

原文链接www.cnblogs.com/zhouzhendong/p/AGC031F.html 草率题解 对于每两个相邻位置,把他们拿出来. 如果这两个相邻位置都有确定的值,那么不管他. 然后把所有的这些数拿出来,分为两类,一类是没有被填入的,一类是被填入的. 然后大力DP即可.由于没有被填入的可以任意排列,所以最后还要乘上一个阶乘. 代码 #include <bits/stdc++.h> #define clr(x) memset(x,0,sizeof x) #define For(i,a,

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