We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 673416d commit 709ed82Copy full SHA for 709ed82
leetcode/647.回文子串.py
@@ -1,4 +1,4 @@
1
-# dp[i][j]表示以s[i]开头s[j]结尾的字符串是否为回文串
+# dp[i][j]表示以s[i]开头、s[j]结尾的字符串是否为回文串
2
class Solution(object):
3
def countSubstrings(self, s):
4
n = len(s)
@@ -9,6 +9,11 @@ def countSubstrings(self, s):
9
dp[i][j] = dp[i+1][j-1] and s[i] == s[j]
10
# 计算二维数组的和并减去下三角的和
11
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
17
18
19
# 暴力破解,遍历所有子串,是回文串则加1
@@ -19,4 +24,4 @@ def countSubstrings(self, s):
24
for j in range(i+1, len(s)+1):
20
25
if s[i:j] == s[i:j][::-1]:
21
26
res += 1
22
- return res
27
+ return res
0 commit comments