Skip to content

Commit 0cfe551

Browse files
author
asxy
authored
Update 0647.回文子串.md
回文子串添加java动态规划简单版本的代码
1 parent f848b4f commit 0cfe551

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/0647.回文子串.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,27 @@ class Solution {
267267
return ans;
268268
}
269269
}
270+
271+
```
272+
273+
动态规划:简洁版
274+
```java
275+
class Solution {
276+
public int countSubstrings(String s) {
277+
boolean[][] dp = new boolean[s.length()][s.length()];
278+
279+
int res = 0;
280+
for (int i = s.length() - 1; i >= 0; i--) {
281+
for (int j = i; j < s.length(); j++) {
282+
if (s.charAt(i) == s.charAt(j) && (j - i <= 1 || dp[i + 1][j - 1])) {
283+
res++;
284+
dp[i][j] = true;
285+
}
286+
}
287+
}
288+
return res;
289+
}
290+
}
270291
```
271292

272293
中心扩散法:

0 commit comments

Comments
 (0)