LeetCode——Palindromic Substrings
Question
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
The input string length won't exceed 1000.
Solution
动态规划,依次求解长度为1,2,3...的子字符串是否为回文,用一个二维数组纪录,同时用一个计数器统计回文子字符串的个数。
Code
class Solution {
public:
int countSubstrings(string s) {
if (s.length() == 0)
return 0;
int len = s.length();
table.resize(len);
for (int i = 0; i < len; i++)
table[i].resize(len);
int count = 0;
for (int i = 0; i < len; i++) {
table[i][i] = true;
count++;
}
// 遍历长度为2,3...的子字符串是否为回文
for (int i = 2; i <= len; i++) {
for (int j = 0; j <= len - i; j++) {
int start = j;
int end = j + i - 1;
if (s[start] == s[end]) {
if (start + 1 < end - 1) {
if (table[start + 1][end - 1] == true) {
table[start][end] = true;
count++;
} else {
table[start][end] = false;
}
} else {
table[start][end] = true;
count++;
}
} else {
table[start][end] = false;
}
}
}
return count;
}
vector<vector<bool> > table;
};
LeetCode——Palindromic Substrings的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [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 ...
随机推荐
- RabbitMq入门与基本使用
这两天工作项目中用到了rabbitmq,顺便学习了一下. RabbitMq主要的使用模式有三种:工作队列,发布订阅和RPC远程调用. 1.工作队列 生产者: using System; using R ...
- (N)IO Frameworks in Java
(N)IO Frameworks in Java – Thread.currentThread.join() https://www.ashishpaliwal.com/blog/2008/10/ni ...
- Nagle's Algorithm and TCP_NODELAY
w非全尺寸分组的发送条件 HTTP The Definitive Guide TCP has a data stream interface that permits applications to ...
- Python overall structer
在C/C++/Java中,main是程序执行的起点,Python中,也有类似的运行机制,但方式却截然不同:Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入时 ...
- Kafka简介及使用
一.Kafka概述 离线部分: Hadoop->离线计算(hdfs / mapreduce) yarn zookeeper->分布式协调(动物管理员) hive->数据仓库(离线计算 ...
- 在linux上配置Django项目
依赖包 [root@web01 ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqli ...
- django中使用redis
第一种 安装redis模块 1.1在app中定义一个redis的连接池的py文件 import redis POOL=redis.ConnectionPool(host='127.0.0.1',por ...
- java-mybaits-00402-Mapper-动态sql-if、where、foreach、sql片段
1.动态sql(重点) 通过mybatis提供的各种标签方法实现动态拼接sql. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. ...
- sipp模拟电信运营商VoIP终端测试(SIP协议调试)
三大运营商都有SIP服务器,用来支持语音对讲,多媒体调度等功能,他们的平台可能不是标准的SIP协议会话. 为了应对没完没了的对接各个厂商的平台,这里再整理了一套协议脚本,毕竟全都是没有意义的无用功,标 ...
- Spark2.0机器学习系列之5:随机森林
概述 随机森林是决策树的组合算法,基础是决策树,关于决策树和Spark2.0中的代码设计可以参考本人另外一篇博客: http://www.cnblogs.com/itboys/p/8312894.ht ...