diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" index 5ea16337e40df..5eecf3be5eff7 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" @@ -258,6 +258,38 @@ public class Solution } ``` +#### Swift + +```swift +class Solution { + private var ans: [[Int]] = [] + private var target: Int = 0 + private var candidates: [Int] = [] + + func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { + self.ans = [] + self.target = target + self.candidates = candidates + dfs(0, 0, []) + return ans + } + + private func dfs(_ sum: Int, _ index: Int, _ current: [Int]) { + if sum == target { + ans.append(current) + return + } + if sum > target { + return + } + for i in index.. diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.swift" new file mode 100644 index 0000000000000..ad2150ec3f31c --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.swift" @@ -0,0 +1,27 @@ +class Solution { + private var ans: [[Int]] = [] + private var target: Int = 0 + private var candidates: [Int] = [] + + func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { + self.ans = [] + self.target = target + self.candidates = candidates + dfs(0, 0, []) + return ans + } + + private func dfs(_ sum: Int, _ index: Int, _ current: [Int]) { + if sum == target { + ans.append(current) + return + } + if sum > target { + return + } + for i in index..