● 647. 回文子串
class Solution { public int countSubstrings(String s) { char[] chars = s.toCharArray(); int len = chars.length; boolean[][] dp = new boolean[len][len]; int result = 0; for(int i = len-1;i>=0;i--){ for(int j =i;j<len;j++){ if(chars[i] == chars[j]){ if(j - i <= 1){ result++; dp[i][j] = true; }else if(dp[i+1][j-1]){ result++; dp[i][j] = true; } } } } return result; } }
● 516.最长回文子序列
class Solution { public int longestPalindromeSubseq(String s) { int len = s.length(); int[][] dp = new int[len+1][len+1]; for(int i = len-1;i>=0;i--){ dp[i][i] = 1; for(int j = i+1;j<len;j++){ if(s.charAt(i) == s.charAt(j)){ dp[i][j] = dp[i+1][j-1] + 2; }else{ dp[i][j] = Math.max(dp[i+1][j],Math.max(dp[i][j],dp[i][j-1])); } } } return dp[0][len-1]; } }
● 动态规划总结篇文章来源:https://www.toymoban.com/news/detail-594560.html
难文章来源地址https://www.toymoban.com/news/detail-594560.html
到了这里,关于Day 57|647. 回文子串| 516.最长回文子序列的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!