LeetCode-647.回文子串

647.回文子串 -点击查看题目要求
解题思路:从中心向两侧延伸
字符串的回文子串的中心位置有2N个(从0-2N-1)
根据中心位置的不同,从最小的回文子串慢慢向外延伸

class Solution {
     public int countSubstrings(String s) {
		  
		  int N = 2*s.length()-1;
		  int num = 0;
		  for(int center = 0; center < N; center++) {
			  int left = center/2;
			  int right = left+center%2;
			  while(left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)) {
				  num++;
				  left--;
				  right++;
			  }
			  
		  }
		 
		  return num;
	        
	    }

}
发布了1 篇原创文章 · 获赞 0 · 访问量 5

猜你喜欢

转载自blog.csdn.net/weixin_37613476/article/details/104283432
今日推荐