'示例 1: '输入: nums = [1,1,2] '输出: [[1,1,2], [1,2,1], [2,1,1]] '示例 2: '输入: nums = [1,2,3] '输出: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Public path As New Collection Public dic As New Dictionary Public used(2) As Boolean Public k As Long Public res_str Sub 回溯算法_全排列_元素重复_字典去重() k = 0 Sheet7.Cells.ClearContents nums = Array(1, 1, 2) nums = Arrays.sort(nums) Call permuteHelper(nums) Set dic = Nothing End Sub Public Sub permuteHelper(nums) If path.count = UBound(nums) + 1 Then k = k + 1 s = path(1) & path(2) & path(3) dic(s) = "" Sheet7.Range("a1").Resize(dic.count, 1) = Application.Transpose(dic.Keys) End If For i = 0 To UBound(nums) If used(i) = False Then used(i) = True path.Add (nums(i)) permuteHelper (nums) path.Remove (path.count) used(i) = False End If Next End Sub
标签:End,Sub,nums,dic,重法,回溯,path,Public,字典 From: https://www.cnblogs.com/eyunkeji/p/16968839.html