From 0cfe55180439c77995abefaa02c5b61108203f3a Mon Sep 17 00:00:00 2001 From: asxy <17375702582@163.com> Date: Thu, 20 Apr 2023 11:14:43 +0800 Subject: [PATCH] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回文子串添加java动态规划简单版本的代码 --- ...36\346\226\207\345\255\220\344\270\262.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 90e6da9fbf..521c8c268e 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -267,6 +267,27 @@ class Solution { 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; + } +} ``` 中心扩散法: