Open youngyangyang04 opened 2 months ago
如果字符 s[i] 与字符 s[j] 相同,那么:
若 j - i = 0,例如 1 - 1 = 0,则 a 是回文子串
若 j - i = 1,例如 2 - 1 = 1,则 aa 是回文子串
若 j - i > 1,且 i+1 到 j-1 也是回文子串。例如 b 是回文子串,则 aba 也是。
// 例: s = "aaa"
i = 2:
j = 2: 满足 'a' == 'a', 满足 2-2 <= 1 (情况一), result = 1, dp[2][2] = t
i = 1:
j = 1: 满足 'a' == 'a', 满足 1-1 <= 1 (情况一), result = 2, dp[1][1] = t
j = 2: 满足 'a' == 'a', 满足 2-1 <= 1 (情况二), result = 3, dp[1][2] = t
i = 0:
j = 0: 满足 'a' == 'a', 满足 0-0 <= 1 (情况一), result = 4, dp[0][0] = t
j = 1: 满足 'a' == 'a', 满足 1-0 <= 1 (情况二), result = 5, dp[0][1] = t
j = 2: 满足 'a' == 'a', 满足 dp[1][1] = t (情况三), result = 6, dp[0][2] = t
https://www.programmercarl.com/0647.%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.html