LeetCode 647. Palindromic Substrings[Medium]

来源:互联网 发布:博罗县网络问政中心 编辑:程序博客网 时间:2024/06/06 02:28

原题地址

题目内容

这里写图片描述

题目分析

求出当前字符串中有多少回文子串,从不同回文的中心向外扩展,计数。

代码实现

public int countSubstrings(String s) {      int res = 0, n = s.length();      for(int i = 0; i<n ;i++ ){          for(int j = 0; i-j >= 0 && i+j < n && s.charAt(i-j) == s.charAt(i+j); j++){              res++;          }          for(int j = 0; i-1-j >= 0 && i+j < n && s.charAt(i-1-j) == s.charAt(i+j); j++){              res++;         }      }      return res;  }  
原创粉丝点击