Skip to content

Commit 709ed82

Browse files
authored
Update 647.回文子串.py
1 parent 673416d commit 709ed82

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

leetcode/647.回文子串.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# dp[i][j]表示以s[i]开头s[j]结尾的字符串是否为回文串
1+
# dp[i][j]表示以s[i]开头、s[j]结尾的字符串是否为回文串
22
class Solution(object):
33
def countSubstrings(self, s):
44
n = len(s)
@@ -9,6 +9,11 @@ def countSubstrings(self, s):
99
dp[i][j] = dp[i+1][j-1] and s[i] == s[j]
1010
# 计算二维数组的和并减去下三角的和
1111
return sum(map(sum, dp)) - (n*(n-1)/2)
12+
# a 0 0 0 1
13+
# 1 b 0 1 0
14+
# 1 1 c 0 0
15+
# 1 1 1 b 0
16+
# 1 1 1 1 a
1217

1318

1419
# 暴力破解,遍历所有子串,是回文串则加1
@@ -19,4 +24,4 @@ def countSubstrings(self, s):
1924
for j in range(i+1, len(s)+1):
2025
if s[i:j] == s[i:j][::-1]:
2126
res += 1
22-
return res
27+
return res

0 commit comments

Comments
 (0)