mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 02:21:30 +08:00
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
This commit is contained in:
@@ -13,7 +13,7 @@ func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res:
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for i in stride(from: start, to: choices.count, by: 1) {
|
||||
for i in choices.indices.dropFirst(start) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if target - choices[i] < 0 {
|
||||
|
||||
@@ -12,7 +12,7 @@ func backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res:
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for i in stride(from: 0, to: choices.count, by: 1) {
|
||||
for i in choices.indices {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if total + choices[i] > target {
|
||||
continue
|
||||
|
||||
@@ -14,7 +14,7 @@ func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res:
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for i in stride(from: start, to: choices.count, by: 1) {
|
||||
for i in choices.indices.dropFirst(start) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if target - choices[i] < 0 {
|
||||
|
||||
Reference in New Issue
Block a user