mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
0131.分割回文串:Swift实现优化迭代中跳过逻辑
This commit is contained in:
@ -575,12 +575,9 @@ func partition(_ s: String) -> [[String]] {
|
|||||||
|
|
||||||
for i in startIndex ..< s.count {
|
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])
|
let substring = String(s[startIndex ... i])
|
||||||
path.append(substring)
|
path.append(substring) // 处理
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串
|
backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串
|
||||||
if !path.isEmpty { path.removeLast() } // 回溯
|
if !path.isEmpty { path.removeLast() } // 回溯
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user