From 3e6dc44fa1965cbaafbb4b3f9119b1a40183a481 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 25 Jul 2024 10:36:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?131.=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E4=B8=B2=E5=A2=9E=E5=8A=A0Go=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=E4=BC=98=E5=8C=96=E5=9B=9E=E6=96=87=E4=B8=B2=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 4a868651..4eca0ddf 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -527,6 +527,7 @@ class Solution: ``` ### Go +回溯 基本版 ```go var ( path []string // 放已经回文的子串 @@ -565,6 +566,63 @@ func isPalindrome(s string) bool { } ``` +回溯+动态规划优化回文串判断 +```go +var ( + result [][]string + path []string // 放已经回文的子串 + isPalindrome [][]bool // 放事先计算好的是否回文子串的结果 +) + +func partition(s string) [][]string { + result = make([][]string, 0) + path = make([]string, 0) + computePalindrome(s) + backtracing(s, 0) + return result +} + +func backtracing(s string, startIndex int) { + // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了 + if startIndex >= len(s) { + tmp := make([]string, len(path)) + copy(tmp, path) + result = append(result, tmp) + return + } + for i := startIndex; i < len(s); i++ { + if isPalindrome[startIndex][i] { // 是回文子串 + // 获取[startIndex,i]在s中的子串 + path = append(path, s[startIndex:i+1]) + } else { // 不是回文,跳过 + continue + } + backtracing(s, i + 1) // 寻找i+1为起始位置的子串 + path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串 + } +} + +func computePalindrome(s string) { + // isPalindrome[i][j] 代表 s[i:j](双边包括)是否是回文字串 + isPalindrome = make([][]bool, len(s)) + for i := 0; i < len(isPalindrome); i++ { + isPalindrome[i] = make([]bool, len(s)) + } + for i := len(s)-1; i >= 0; i-- { + // 需要倒序计算, 保证在i行时, i+1行已经计算好了 + for j := i; j < len(s); j++ { + if j == i { + isPalindrome[i][j] = true + } else if j - i == 1 { + isPalindrome[i][j] = s[i] == s[j] + } else { + isPalindrome[i][j] = s[i] == s[j] && isPalindrome[i+1][j-1] + } + } + } +} +``` + ### JavaScript ```js From 11ddcbdac4667d27cc730fba2d35bfa9ec6bb66c Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 25 Jul 2024 11:30:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?93.=E5=A4=8D=E5=8E=9FIP=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 73d5e3c3..d1300a39 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -143,7 +143,7 @@ for (int i = startIndex; i < s.size(); i++) { 代码如下: ```CPP -// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 +// 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法 bool isValid(const string& s, int start, int end) { if (start > end) { return false; @@ -208,7 +208,7 @@ private: } else break; // 不合法,直接结束本层循环 } } - // 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 + // 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法 bool isValid(const string& s, int start, int end) { if (start > end) { return false;