From d010b09dbd6c65fea927dabf39d10045a5a741da Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:18:11 -0700 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 python版本剪枝去重,避免对递减序列的比较 --- problems/0031.下一个排列.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 88fbb2fc..34aa1086 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -168,7 +168,8 @@ class Solution: Do not return anything, modify nums in-place instead. """ length = len(nums) - for i in range(length - 1, -1, -1): + for i in range(length - 2, -1, -1): # 从倒数第二个开始 + if nums[i]>=nums[i+1]: continue # 剪枝去重 for j in range(length - 1, i, -1): if nums[j] > nums[i]: nums[j], nums[i] = nums[i], nums[j]