转载地址

https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5


题目详情

给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。

具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。

示例 1:

输入: "abc"
输出: 3
解释: 三个回文子串: "a", "b", "c".

示例 2:

输入: "aaa"
输出: 6
说明: 6个回文子串: "a", "a", "a", "aa", "aa", "aaa".

注意:

  1. 输入的字符串长度不会超过1000。

题目分析

一个小问题,子串(Substring)、子数组(Subarray)和子序列(Subsequence)的区别:子串和子数组是等同的,特点是连续的,比如[1,2,3]的子串有(1), (2), (3), (1,2), (2,3), (1,2,3)。而子序列不一定相邻,但相对顺序一致,比如(1,3)是[1,2,3]的一个子序列。

方法有很多种,简单讲一些。


方法一: DP

一开始定义DP[i][j]为i、j之间的回文子串数,很是麻烦,还需要另外的数组记录子串[i, j]是否是回文的。其实没有必要,直接将DP[i][j]定义成子串[i, j]是否是回文串。外循环 i从 n−1 往 0 遍历,内循环 j 从 i 往 n−1 遍历,若s[i]==s[j]:

若i==j,则dp[i][j]=true;

若i和j是相邻的,则dp[i][j]=true;

若i和j中间只有一个字符,则dp[i][j]=true;

否则,检查dp[i+1][j-1]是否为true,若为true,那么dp[i][j]就是true。

前三条可以合并,即 j−i≤2。求得dp[i][j]真值后,如果其为true,最终结果res++。

时间复杂度:O(n^2)。

方法一参考代码:

class Solution {
public:
int countSubstrings(string s) {
int len = s.size(), res = 0;
vector<vector<bool>> dp(len, vector<bool>(len, false));
for (int i = len - 1; i >= 0; --i) {
for (int j = i; j < len; ++j) {
dp[i][j] = (s[i] == s[j]) && (j - i <= 2 || dp[i + 1][j - 1]);
if (dp[i][j]) ++res;
}
}
return res;
}
};

方法二:回文中心法

本题可以不用DP,而是采用一种巧妙的方法:回文中心法。什么意思呢?考虑不同的回文中心,往两边扩散,求得回文数。需要考虑两种情况:如果是奇数长度回文串,了么回文中心为最中间的一个字符;如果是偶数长度回文串,这回文中心为最中间的两个字符。

每个回文子串只有一个回文中心,所以这种方法不会重复计算,也不会漏算。

时间复杂度:O(n^2)。

方法二参考代码:

class Solution {
public:
int countSubstrings(string s) {
int len = s.size(), res = 0;
for (int i = 0; i < len; ++i) {
int mid1 = i, mid2 = i;//奇数
while (mid1 >= 0 && mid2 < len && s[mid1] == s[mid2]) {
--mid1; ++mid2; ++res;
} mid1 = i, mid2 = i+1;//偶数
while (mid1 >= 0 && mid2 < len && s[mid1] == s[mid2]) {
--mid1; ++mid2; ++res;
}
}
return res;
}
};

方法三:“马拉车”算法

神奇的算法,先马一下,学会再写上。听说时间复杂度是 O(n)。

好了,学到了,请参考:什么是马拉车算法?

利用马拉车算法,可以得到所有情况下的最大半径,以s[i]为中心,RL[i]为半径的回文串中含有的字回文串数目是 RL[i]/2 个。

方法三参考代码:

class Solution {
public:
int countSubstrings(string s) {
//预处理
string t = "#";
for (int i = 0; i < s.size(); ++i) {
t += s[i];
t += "#";
} vector<int> RL(t.size(), 0);
int MaxRight = 0, pos = 0;
int res = 0;
for (int i = 0; i < t.size(); ++i) {
RL[i] = MaxRight > i ? min(RL[2 * pos - i], MaxRight - i) : 1; while (i-RL[i] >=0 && i+RL[i] < t.size() && t[i + RL[i]] == t[i - RL[i]])//扩展,注意边界
++RL[i];
//更新最右端及其中心
if (MaxRight < i + RL[i] -1) {
MaxRight = i + RL[i] -1;
pos = i;
} res += RL[i]/2;
}
return res;
}
};

LeetCode 647. Palindromic Substrings的三种解法的更多相关文章

  1. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  3. LeetCode算法题-Move Zeroes(Java实现-三种解法)

    这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同 ...

  4. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  5. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  6. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  7. [LeetCode] Remove Element (三种解法)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. 【Leetcode】647. Palindromic Substrings

    Description Given a string, your task is to count how many palindromic substrings in this string. Th ...

  9. 647. Palindromic Substrings 互文的子字符串

    [抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...

随机推荐

  1. Burp Suite Decoder Module - 解码模块

    官方参考链接:https://portswigger.net/burp/documentation/desktop/tools/decoder 该模块主要进行编码和解码,支持编码方式有:Plain,U ...

  2. Python Ethical Hacking - The Lab and Needed Software

    The Lab and Needed Software Attacker Machine - Kali Linux https://www.kali.org/ 1. Install the softw ...

  3. P1469 找筷子

    摘要:有n根(n为奇数)长短不一的筷子,里面可以凑成(n-1)/2双筷子,只剩下一根不能凑对,问那根不能凑对的筷子有多长. 乍听起来好像不难,桶是一个好东西,可是一看数据:对于100%的数据,N< ...

  4. SQL 给某字段添加汉字却显示??

    错误展示: 解决方案: 1.在要修改的数据库上单击鼠标右键,并选择“属性”.   2.在弹出的数据库属性窗口中点击“选择页”中的“选项”.   3.将排序规则由默认的SQL_Latin1_Genera ...

  5. F - Maximal Intersection --------暴力求解题

    You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...

  6. 【C++】初次学习C++指针时的一些易混或疑惑的地方

    C++中的指针是一个比较复杂的知识概念,最近我有在学习这一方面的知识,就借此文章记录一下在学习时容易产生的混淆.本人初次发技术类的分享,可能会有纰漏,欢迎诸位指正^_^! 1.*在两种语境下的含义 先 ...

  7. vue学习(六) 事件修饰符 stop prevent capture self once

    //html <div id="app"> <div @click="divHandler" style="height:150px ...

  8. 【Laravel】为Eloquent 模型设置全局作用域和局部作用域进行查询

    全局作用域 所谓「全局作用域」,指的是预置过滤器在注册该「全局作用域」的模型类的所有查询中生效,不需要指定任何额外条件. 以 User 模型类为例,我们在系统中可能只想针对已经验证过邮箱的用户进行操作 ...

  9. Centos 安装ixgbe驱动

    1. 首先查看网卡类型 [root]# lspci | grep 8259907:00.0 Ethernet controller: Intel Corporation 82599ES 10-Giga ...

  10. IDEA 修改快捷键和Myeclipse 快捷键一致

    介绍 我们知道IDEA这款开发工具功能很强大,为了简化开发步骤,提高开发效率,使用快捷键很显然是必不可少的,那么怎么才能使得IDEA快捷键和MyEclipse快捷键 保持相同呢? 第一种方法,一个快捷 ...