From ca00ec680557689d6b302fe27b3e66e479a0d421 Mon Sep 17 00:00:00 2001 From: xiaojun <13589818805@163.com> Date: Mon, 4 Jul 2022 15:43:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0031.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=8E=92=E5=88=97.md)go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0031.下一个排列.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index bce8adef..1a3641b0 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -171,13 +171,13 @@ class Solution(object): 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] @@ -190,6 +190,26 @@ class Solution(object): ## Go ```go +//卡尔的解法 +func nextPermutation(nums []int) { + for i:=len(nums)-1;i>=0;i--{ + for j:=len(nums)-1;j>i;j--{ + if nums[j]>nums[i]{ + //交换 + nums[j],nums[i]=nums[i],nums[j] + reverse(nums,0+i+1,len(nums)-1) + return + } + } + } + reverse(nums,0,len(nums)-1) +} +//对目标切片指定区间的反转方法 +func reverse(a []int,begin,end int){ + for i,j:=begin,end;i