From 912e27442fa5d71c4848d345dc56e1042a666032 Mon Sep 17 00:00:00 2001 From: Carol Date: Tue, 9 Nov 2021 12:03:32 +0800 Subject: [PATCH] =?UTF-8?q?Update=200031.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0031.下一个排列.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 9999486e..a6aa8a91 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -165,6 +165,29 @@ class Solution: low += 1 high -= 1 ``` +>上一版本简化版 +'''python +class Solution(object): + def nextPermutation(self, nums: List[int]) -> None: + n = len(nums) + i = n-2 + while i >= 0 and nums[i] >= nums[i+1]: + i -= 1 + + if i > -1: // i==-1,不存在下一个更大的排列 + j = n-1 + while j >= 0 and nums[j] <= nums[i]: + j -= 1 + nums[i], nums[j] = nums[j], nums[i] + + start, end = i+1, n-1 + while start < end: + nums[start], nums[end] = nums[end], nums[start] + start += 1 + end -= 1 + + return nums +''' ## Go