LeetCode题解之Palindromic Substrings

1、问题描述

 

2、问题分析

对于每一个字符,以该字符为中心计算回文个数。

3、代码

 1 int countSubstrings(string s) {
 2         int count = 0;
 3         if( s.size() == 0)
 4             return 0;
 5         
 6         for( int i = 0; i < s.size(); i++){
 7             count += checkPalindromic(s, i,i);
 8             count += checkPalindromic(s, i, i+1);
 9         }
10         
11         return count ;
12     }
13     
14     int checkPalindromic( string s ,int i ,int j ){
15         int count = 0;
16         while( i >=0 && j < s.size() && s[i] == s[j]){
17             i--;
18             j++;
19             count++;
20         }
21         return count;
22     }

 

posted @ 2018-09-03 15:57  山里的小勇子  阅读(134)  评论(0编辑  收藏  举报