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"));
}
}
LeetCode-Palindromic Substrings的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode——Palindromic Substrings
Question Given a string, your task is to count how many palindromic substrings in this string. The s ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 647. 回文子串(Palindromic Substrings)
647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- Leetcode 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...
- [Swift]LeetCode647. 回文子串 | Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
随机推荐
- Java内存模型:volatile详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt202 Java内存模型:volatile是干什么用的Volatile字段是用 ...
- C# XmlDocument操作XML
XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...
- 大数的减法函数--c语言
代码展示: http://paste.ubuntu.com/23693598/ #include<stdio.h> #include<stdlib.h> #include& ...
- 201521123096《Java程序设计》第二周学习总结
1.本周学习总结 (1)学会使用码云管理代码: (2)了解数组和字符串的操作: (3)对完全限定类名有一定的认识. 2.书面作业 (1)使用Eclipse关联jdk源代码,并查看String对象的源代 ...
- [转载]sqlserver、Mysql、Oracle三种数据库的优缺点总结
一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能.与许多其他服务器软件紧密关联的集成性.良好的性价比等:为数据管理与分析带来了灵活性,允许单位在快速变化的环境中 ...
- evak购物车--课程设计(201521123037邱晓娴)
1. 团队课程设计博客链接 团队博客 2. 个人负责模块或任务说明 1.Java (1)编写用户类Users (2)编写DBConnection类,连接数据库 (3)编写GoodsDAO类,从数据库中 ...
- Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】
配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...
- JDBC在javaweb中的应用之分页数据查询
分页查询 分页查询是java web开发中经常使用到的技术.在数据库中数据量非常大的情况下,不适合将所有的数据全部显示到一个页面中,同时为了节约程序以及数据库的资源,就需要对数据进行分页查询操作. 通 ...
- Ansible系列(七):执行过程分析、异步模式和速度优化
本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...
- iOS开发者的管理工具-CocoaPods安装
1. 安装 Ruby 对于iOS开发者,CocoaPods是最方便使用的第三方管理工具了,但是怎么安装CocoaPods呢,安装CocoaPods之前,要确保mac已经安装上Ruby,但在安装Ruby ...