'示例 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 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)
End Sub
Public Sub permuteHelper(nums)
If path.count = UBound(nums) + 1 Then
k = k + 1
s = path(1) & path(2) & path(3)
Sheet7.Cells(k, 1) = s
End If
For i = 0 To UBound(nums)
If i > 0 Then
If nums(i) = nums(i - 1) And used(i - 1) = False Then GoTo 10000
End If
If used(i) = False Then
used(i) = True
path.Add (nums(i))
permuteHelper (nums)
path.Remove (path.count)
used(i) = False
End If
10000
Next
End Sub