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 ...
随机推荐
- Favorite Donut--hdu5442(2015年长春网选赛,kmp,最大表示法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 打比赛的时候还没学kmp更没有学最大最小表示法,之后做完了kmp的专题,学了它们,现在再来做这道 ...
- 持续集成之戏说Check-in Dance(转)
add by zhj: 先说一下持续集成的定义,这是ThoughtWorks首席科学家Martin Fowler在<持续集成>第二版中给出的,“持续集成是一种软件开发实践.在持续集成中,团 ...
- Flume 1.7 源代码分析(四)从Source写数据到Channel
Flume 1.7 源代码分析(一)源代码编译 Flume 1.7 源代码分析(二)总体架构 Flume 1.7 源代码分析(三)程序入口 Flume 1.7 源代码分析(四)从Source写数据到C ...
- SpringBoot安装和创建简单的Web应用
SpringBoot安装 方式一: Eclipese->Help->Eclipse Marketplace ->Finde STS -> Install 注意:安装过程中挺慢, ...
- MBR和GPT(分区)
MBR:Master Boot Record GPT:Guid Partition Table (全局唯一标识符分区表) GPT is the new standard and is graduall ...
- js中将Object转换为String函数代码
经常会碰到结果对象是object而无法查看该对象里面的内容而苦恼,有下面这个函数就好了,可以将其转化为字符串类型,然后就可以打印出来了,具体代码如下: function obj2string(o){ ...
- 1141 PAT Ranking of Institutions[难]
1141 PAT Ranking of Institutions (25 分) After each PAT, the PAT Center will announce the ranking of ...
- 关于LDA的gibbs采样,为什么可以获得正确的样本?
算法里面是随机初始了一个分布,然后进行采样,然后根据每次采样的结果去更新分布,之后接着采样直到收敛. 1.首先明确一下MCMC方法. 当我们面对一个未知或者复杂的分布时,我们经常使用MCMC方法来进行 ...
- visual studio NuGet
http://www.cnblogs.com/dudu/archive/2011/07/15/nuget.html 首先打开程序包管理器控制台:工具→Nuget程序包管理器→程序包管理器控制台 Ins ...
- boost circular buffer环形缓冲类
Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据. 它是一个与STL兼容的容器,类似于 std::list或std::de ...