算法 - 回溯02(Swift版本)

83 阅读1分钟

题目1:39. 组合总和

讲解     
leetcode

class Solution {
    var result = [[Int]]()
    var path = [Int]()
    func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
        combinationSumRecusive(candidates.sorted(), target, 0, 0)
        return result
    }
    func combinationSumRecusive(_ candidates: [Int], _ target: Int, _ start: Int,_ sum: Int) {
        if sum >= target {
            if sum == target { 
                result.append(path)
            }
            return 
         }
         for index in start..<candidates.count {
            let item = candidates[index]
            if sum + item > target { break }
            path.append(item)
            combinationSumRecusive(candidates, target, index, sum + item)
            path.removeLast()
         }
    }
}

题目2: 40.组合总和II

讲解     
leetcode

树层去重 和 树枝去重的说法还是挺贴切的。


// 去重写法1 时间效率更高一些。
class Solution {
    var result = [[Int]]()
    var path = [Int]()
    func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
        combinationSum2Recursive(candidates.sorted(), target, 0, 0)        
        return result
    }

    func combinationSum2Recursive(_ candidates: [Int], _ target: Int, _ start: Int, _ sum: Int) {
        if sum >= target {
            if sum == target { result.append(path) }             
            return
        }
        var pre = 0
        for i in start..<candidates.count {
            let num = candidates[i]
            // 剪枝
            if sum + num > target { break }
            // 去重
            if pre > 0, num == pre { continue }
            pre = num
            path.append(num)
            combinationSum2Recursive(candidates, target, i + 1, sum + num)
            path.removeLast()
        }
    }
}

// 去重写法2
class Solution {
    var result = [[Int]]()
    var path = [Int]()
    func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
        combinationSum2Recursive(candidates.sorted(), target, 0, 0)        
        return result
    }

    func combinationSum2Recursive(_ candidates: [Int], _ target: Int, _ start: Int, _ sum: Int) {
        if sum >= target {
            if sum == target { result.append(path) }             
            return
        }
        for i in start..<candidates.count {
            let num = candidates[i]
            // 剪枝
            if sum + num > target { break }
            // 去重
            if i > start, num == candidates[i - 1] { continue }
            path.append(num)
            combinationSum2Recursive(candidates, target, i + 1, sum + num)
            path.removeLast()
        }
    }
}

题目3: 131.分割回文串

讲解     
leetcode

// @lc code=start
class Solution {
    func isPalindrome(_ s: [Character]) -> Bool {
        if s.count == 0 { return false }
        var j = s.count - 1
        for i in 0..<s.count {
            if s[i] != s[j] {
                return false
            }
            j -= 1            
        }
        return true
    }
    var result = [[String]]()
    var path = [String]()
    func partition(_ s: String) -> [[String]] {
        partitionRecursive(s, 0)
        return result
    }

    func partitionRecursive(_ s: String, _ start: Int) {
        if start >= s.count { 
            result.append(path)
            return
        }
        for i in start..<s.count {
            let startIdx = s.index(s.startIndex, offsetBy: start)
            let endIdx = s.index(s.startIndex, offsetBy: i + 1)
            let subString = String(s[startIdx..<endIdx])
            print("\(subString)")
            if !isPalindrome(Array(subString)) {
                continue
            }
            path.append(subString)
            partitionRecursive(s, i + 1)
            path.removeLast()
        }
    }
}
// @lc code=end