## 1、题目
https://leetcode-cn.com/problems/palindromic-substrings/submissions/
2、题意
题解1:写了个暴力 虽然过了 时间复杂度很高
class Solution {
public:
bool is_huiwen(string temp)
{
int l = 0,r = temp.size()-1;
while(l<r)
{
if(temp[l]==temp[r])
{
l++,r--;
}
else
return false;
}
return true;
}
int countSubstrings(string s) {
int res = s.size();
for(int i=0;i<s.size()-1;i++)
{
int t = 2;
while(i+t<=s.size())
{
if(is_huiwen(s.substr(i,t)))
res++;
t++;
}
}
return res;
}
};
题解2:中心扩展 分别以i为中心和以(i,i+1)为中心扩展
class Solution {
public:
int res = 0;
void helper(string s,int i,int j)
{
while(i>=0&&j<s.size()&&s[i]==s[j])
{
i--;
j++;
res++;
}
}
int countSubstrings(string s) {
for(int i=0;i<s.size();i++)
{
helper(s,i,i);
helper(s,i,i+1);
}
return res;
}
};
class Solution {
public:
int countSubstrings(string s) {
int res = 0, n = s.size();
for(int i=0;i<n;i++)
{
for(int j=0;i-j>=0&&i+j<n&&s[i-j]==s[i+j];j++)
res++;
for(int j=0;i-j>=0&&i+j+1<n&&s[i-j]==s[i+j+1];j++)
res++;
}
return res;
}
};``