class Solution {
public:
int count = 0;
int countSubstrings(string s) {
for(int i = 0; i < s.size(); i++){
fun_helper(s, i, i); //分两种情况,以自己为中心往两边扩散
fun_helper(s, i, i + 1); //以自己和右的旁边,往两边扩散
}
return count;
}
void fun_helper(string s, int begin, int end){
while(begin >= 0 && end <= s.size()){
if(s[begin] == s[end]){
count++;
begin--;
end++;
}else{
break;
}
}
}
};
647. Palindromic Substrings ( )
最新推荐文章于 2025-04-21 23:32:35 发布
本文介绍了一个使用C++实现的高效算法,用于计算字符串中所有回文子串的数量。通过定义一个Solution类,包含countSubstrings成员函数,该函数遍历输入字符串并利用fun_helper辅助函数检查每个可能的回文子串,从而实现计数。此算法考虑了以单个字符或相邻字符为中心的两种回文情况。
1753

被折叠的 条评论
为什么被折叠?



