feat: add Swift codes for subset_sum_problem article (#584)

pull/586/head
nuomi1 1 year ago committed by GitHub
parent b4de2c0d13
commit de4f8378a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -68,6 +68,9 @@ let package = Package(
.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"]),
.executable(name: "subset_sum_i_naive", targets: ["subset_sum_i_naive"]),
.executable(name: "subset_sum_i", targets: ["subset_sum_i"]),
.executable(name: "subset_sum_ii", targets: ["subset_sum_ii"]),
.executable(name: "n_queens", targets: ["n_queens"]),
],
targets: [
@ -137,6 +140,9 @@ let package = Package(
.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"]),
.executableTarget(name: "subset_sum_i_naive", path: "chapter_backtracking", sources: ["subset_sum_i_naive.swift"]),
.executableTarget(name: "subset_sum_i", path: "chapter_backtracking", sources: ["subset_sum_i.swift"]),
.executableTarget(name: "subset_sum_ii", path: "chapter_backtracking", sources: ["subset_sum_ii.swift"]),
.executableTarget(name: "n_queens", path: "chapter_backtracking", sources: ["n_queens.swift"]),
]
)

@ -0,0 +1,53 @@
/**
* File: subset_sum_i.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* I */
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
// target
if target == 0 {
res.append(state)
return
}
//
// start
for i in stride(from: start, to: choices.count, by: 1) {
// target
// target
if target - choices[i] < 0 {
break
}
// target, start
state.append(choices[i])
//
backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)
// 退
state.removeLast()
}
}
/* I */
func subsetSumI(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let nums = nums.sorted() // nums
let start = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
return res
}
@main
enum SubsetSumI {
/* Driver Code */
static func main() {
let nums = [3, 4, 5]
let target = 9
let res = subsetSumI(nums: nums, target: target)
print("输入数组 nums = \(nums), target = \(target)")
print("所有和等于 \(target) 的子集 res = \(res)")
}
}

@ -0,0 +1,51 @@
/**
* File: subset_sum_i_naive.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* I */
func backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {
// target
if total == target {
res.append(state)
return
}
//
for i in stride(from: 0, to: choices.count, by: 1) {
// target
if total + choices[i] > target {
continue
}
// total
state.append(choices[i])
//
backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)
// 退
state.removeLast()
}
}
/* I */
func subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let total = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, total: total, choices: nums, res: &res)
return res
}
@main
enum SubsetSumINaive {
/* Driver Code */
static func main() {
let nums = [3, 4, 5]
let target = 9
let res = subsetSumINaive(nums: nums, target: target)
print("输入数组 nums = \(nums), target = \(target)")
print("所有和等于 \(target) 的子集 res = \(res)")
print("请注意,该方法输出的结果包含重复集合")
}
}

@ -0,0 +1,58 @@
/**
* File: subset_sum_ii.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* II */
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
// target
if target == 0 {
res.append(state)
return
}
//
// start
// start
for i in stride(from: start, to: choices.count, by: 1) {
// target
// target
if target - choices[i] < 0 {
break
}
//
if i > start, choices[i] == choices[i - 1] {
continue
}
// target, start
state.append(choices[i])
//
backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)
// 退
state.removeLast()
}
}
/* II */
func subsetSumII(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let nums = nums.sorted() // nums
let start = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
return res
}
@main
enum SubsetSumII {
/* Driver Code */
static func main() {
let nums = [4, 4, 5]
let target = 9
let res = subsetSumII(nums: nums, target: target)
print("输入数组 nums = \(nums), target = \(target)")
print("所有和等于 \(target) 的子集 res = \(res)")
}
}
Loading…
Cancel
Save