Medium
Given an array nums
of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
Constraints:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums
are unique.To solve the “Permutations” problem in Swift with a Solution
class, we can follow these steps:
Solution
class.permute
that takes an array of distinct integers nums
as input and returns a list of all possible permutations.permuteHelper
to generate permutations.permuteHelper
function:
nums
, add a copy of the current permutation to the result list.nums
:
permuteHelper
with the updated permutation and the remaining elements of nums
.Here’s the implementation:
public class Solution {
public func permute(_ nums: [Int]) -> [[Int]] {
if nums.isEmpty {
return []
}
var finalResult = \[\[Int]]()
var currResult = [Int]()
var used = [Bool](repeating: false, count: nums.count)
permuteRecur(nums, &finalResult, &currResult, &used)
return finalResult
}
private func permuteRecur(_ nums: [Int], _ finalResult: inout [[Int]], _ currResult: inout [Int], _ used: inout [Bool]) {
if currResult.count == nums.count {
finalResult.append(currResult)
return
}
for i in 0..<nums.count {
if used[i] {
continue
}
currResult.append(nums[i])
used[i] = true
permuteRecur(nums, &finalResult, &currResult, &used)
used[i] = false
currResult.removeLast()
}
}
}
This implementation provides a solution to the “Permutations” problem in Swift. It generates all possible permutations of the given array of distinct integers using backtracking.