zoukankan      html  css  js  c++  java
  • LeetCode-Palindromic Substrings

    package Classify.DP.Medium;

    import org.junit.jupiter.api.Test;

    public class PalindromicSubstrings {

    /**
     * 基本思路:这里的 dp 方程的每一个元素就代表我要以当前元素作为回文子串的结尾时候的回文子串的数量
     * 那么递推公式就是以上一个元素结尾时候的子串数量加上本次的结尾的子串的数量就能获得总数量了
     * 而判断当前结尾的回文子串就是判断到对称的元素,然后翻转操作做判断即可
     * @param s
     * @return
     */
    
    public int countSubstrings(String s) {
        int[] dp = new int[s.length()];
        dp[0] = 1;
        for (int i = 1; i < dp.length; i++) {
            dp[i] = dp[i - 1] + currentCount(s, i);
        }
        return dp[s.length() - 1];
    }
    
    private int currentCount(String string, int i) {
        int count = 0;
        for (int j = i; j >= 0; --j) {
            if (string.charAt(i) != string.charAt(j)) {
                continue;
            }
            if (new StringBuilder(string.substring(j, i + 1)).reverse().toString().equals(string.substring(j, i + 1))) {
                ++count;
            }
        }
        return count;
    }
    
    @Test
    public void fun() {
        System.out.println(countSubstrings("aaa"));
    }
    

    }

  • 相关阅读:
    .Net Mvc 基于Filter实现对权限的扩展定制
    VS 2015 远程调试
    SVN 创建版本库
    .NET 调用支付宝沙箱接口
    selenium常用总结
    Python常用小技巧
    Centos7 安装Mysql 5.7
    Sqlserver 递归查询
    Sqlserver 中case when 的详细用法总结
    Asp.NetCoreWebApi入门
  • 原文地址:https://www.cnblogs.com/lwen/p/7657794.html
Copyright © 2011-2022 走看看