mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0647.回文子串.md
回文子串添加java动态规划简单版本的代码
This commit is contained in:
@ -267,6 +267,27 @@ class Solution {
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
动态规划:简洁版
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int countSubstrings(String s) {
|
||||||
|
boolean[][] dp = new boolean[s.length()][s.length()];
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||||||
|
for (int j = i; j < s.length(); j++) {
|
||||||
|
if (s.charAt(i) == s.charAt(j) && (j - i <= 1 || dp[i + 1][j - 1])) {
|
||||||
|
res++;
|
||||||
|
dp[i][j] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
中心扩散法:
|
中心扩散法:
|
||||||
|
Reference in New Issue
Block a user