647. Palindromic Substrings——string

 

题目分析:以每个字母为中间位置,向外拓展,找到所有的回文序列

class Solution(object):
    def repeatedNTimes(self, s):
        count = 0
        for i in range(len(s)):
            count += 1
            # 回文长度是奇数的情况
            left = i - 1
            right = i + 1
            while left >= 0 and right < len(s) and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1
            # 回文长度是偶数的情况
            left = i
            right = i + 1
            while left >= 0 and right < len(s) and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1
        return count

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-02-18
  • 2021-06-12
  • 2022-01-16
  • 2021-10-25
  • 2021-12-02
猜你喜欢
  • 2021-11-28
  • 2021-07-02
  • 2021-12-10
  • 2021-11-16
  • 2022-12-23
相关资源
相似解决方案