From 9ec560d6cd363e427b461d5973985042f31f579d Mon Sep 17 00:00:00 2001 From: nuomi1 Date: Thu, 4 May 2023 05:31:06 +0800 Subject: [PATCH] feat: add Swift codes for permutations_problem article (#482) --- codes/swift/Package.swift | 4 ++ .../chapter_backtracking/permutations_i.swift | 50 ++++++++++++++++++ .../permutations_ii.swift | 52 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 codes/swift/chapter_backtracking/permutations_i.swift create mode 100644 codes/swift/chapter_backtracking/permutations_ii.swift diff --git a/codes/swift/Package.swift b/codes/swift/Package.swift index 7bcffd5df..7df2bf9e2 100644 --- a/codes/swift/Package.swift +++ b/codes/swift/Package.swift @@ -59,6 +59,8 @@ let package = Package( .executable(name: "preorder_traversal_ii_compact", targets: ["preorder_traversal_ii_compact"]), .executable(name: "preorder_traversal_iii_compact", targets: ["preorder_traversal_iii_compact"]), .executable(name: "preorder_traversal_iii_template", targets: ["preorder_traversal_iii_template"]), + .executable(name: "permutations_i", targets: ["permutations_i"]), + .executable(name: "permutations_ii", targets: ["permutations_ii"]), ], targets: [ // helper @@ -118,5 +120,7 @@ let package = Package( .executableTarget(name: "preorder_traversal_ii_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_ii_compact.swift"]), .executableTarget(name: "preorder_traversal_iii_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_iii_compact.swift"]), .executableTarget(name: "preorder_traversal_iii_template", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_iii_template.swift"]), + .executableTarget(name: "permutations_i", path: "chapter_backtracking", sources: ["permutations_i.swift"]), + .executableTarget(name: "permutations_ii", path: "chapter_backtracking", sources: ["permutations_ii.swift"]), ] ) diff --git a/codes/swift/chapter_backtracking/permutations_i.swift b/codes/swift/chapter_backtracking/permutations_i.swift new file mode 100644 index 000000000..51ab05b16 --- /dev/null +++ b/codes/swift/chapter_backtracking/permutations_i.swift @@ -0,0 +1,50 @@ +/** + * File: permutations_i.swift + * Created Time: 2023-04-30 + * Author: nuomi1 (nuomi1@qq.com) + */ + +/* 回溯算法:全排列 I */ +func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) { + // 当状态长度等于元素数量时,记录解 + if state.count == choices.count { + res.append(state) + return + } + // 遍历所有选择 + for (i, choice) in choices.enumerated() { + // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + if !selected[i] { + // 尝试:做出选择,更新状态 + selected[i] = true + state.append(choice) + // 进行下一轮选择 + backtrack(state: &state, choices: choices, selected: &selected, res: &res) + // 回退:撤销选择,恢复到之前的状态 + selected[i] = false + state.removeLast() + } + } +} + +/* 全排列 I */ +func permutationsI(nums: [Int]) -> [[Int]] { + var state: [Int] = [] + var selected = Array(repeating: false, count: nums.count) + var res: [[Int]] = [] + backtrack(state: &state, choices: nums, selected: &selected, res: &res) + return res +} + +@main +enum PermutationsI { + /* Driver Code */ + static func main() { + let nums = [1, 2, 3] + + let res = permutationsI(nums: nums) + + print("输入数组 nums = \(nums)") + print("所有排列 res = \(res)") + } +} diff --git a/codes/swift/chapter_backtracking/permutations_ii.swift b/codes/swift/chapter_backtracking/permutations_ii.swift new file mode 100644 index 000000000..cc36bd6e7 --- /dev/null +++ b/codes/swift/chapter_backtracking/permutations_ii.swift @@ -0,0 +1,52 @@ +/** + * File: permutations_ii.swift + * Created Time: 2023-04-30 + * Author: nuomi1 (nuomi1@qq.com) + */ + +/* 回溯算法:全排列 II */ +func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) { + // 当状态长度等于元素数量时,记录解 + if state.count == choices.count { + res.append(state) + return + } + // 遍历所有选择 + var duplicated: Set = [] + for (i, choice) in choices.enumerated() { + // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + if !selected[i], !duplicated.contains(choice) { + // 尝试:做出选择,更新状态 + duplicated.insert(choice) // 记录选择过的元素值 + selected[i] = true + state.append(choice) + // 进行下一轮选择 + backtrack(state: &state, choices: choices, selected: &selected, res: &res) + // 回退:撤销选择,恢复到之前的状态 + selected[i] = false + state.removeLast() + } + } +} + +/* 全排列 II */ +func permutationsII(nums: [Int]) -> [[Int]] { + var state: [Int] = [] + var selected = Array(repeating: false, count: nums.count) + var res: [[Int]] = [] + backtrack(state: &state, choices: nums, selected: &selected, res: &res) + return res +} + +@main +enum PermutationsII { + /* Driver Code */ + static func main() { + let nums = [1, 2, 3] + + let res = permutationsII(nums: nums) + + print("输入数组 nums = \(nums)") + print("所有排列 res = \(res)") + } +}