0131.分割回文串:Swift实现优化迭代中跳过逻辑

This commit is contained in:
bqlin
2021-12-18 20:23:12 +08:00
parent 622b443bbe
commit 690897f33f

View File

@ -575,12 +575,9 @@ func partition(_ s: String) -> [[String]] {
for i in startIndex ..< s.count {
// 回文则收集,否则跳过
if isPalindrome(start: startIndex, end: i) {
guard isPalindrome(start: startIndex, end: i) else { continue }
let substring = String(s[startIndex ... i])
path.append(substring)
} else {
continue
}
path.append(substring) // 处理
backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串
if !path.isEmpty { path.removeLast() } // 回溯
}