All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0031_next_permutation.Solution.py Maven / Gradle / Ivy

The newest version!
# #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1)
# #2024_06_08_Time_35_ms_(93.06%)_Space_16.5_MB_(44.75%)

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        if not nums or len(nums) <= 1:
            return

        i = len(nums) - 2
        while i >= 0 and nums[i] >= nums[i + 1]:
            i -= 1

        if i >= 0:
            j = len(nums) - 1
            while nums[j] <= nums[i]:
                j -= 1
            self.swap(nums, i, j)

        self.reverse(nums, i + 1, len(nums) - 1)

    def swap(self, nums, i, j):
        nums[i], nums[j] = nums[j], nums[i]

    def reverse(self, nums, i, j):
        while i < j:
            self.swap(nums, i, j)
            i += 1
            j -= 1




© 2015 - 2025 Weber Informatics LLC | Privacy Policy