美文网首页
66. LeetCode 409. 最长回文串

66. LeetCode 409. 最长回文串

作者: 月牙眼的楼下小黑 | 来源:发表于2019-02-16 14:06 被阅读6次
  • 标签: 哈希表
  • 难度: 简单

  • 题目描述
  • 我的解法

对各个字符进行计数, 若计数值 val 等于偶数,则 result += val, 若为奇数, 则 result += val - 1。 若计数值中存在奇数,则最终结果加 1.

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        counter = {}
        for c in s:
            if counter.get(c) == None:
                counter[c] = 1
            else:
                counter[c] += 1
        odd = 0
        result = 0
        for key, val in counter.items():
            if val % 2 == 0:
                result += val
            else:
                result += val - 1
                odd = 1
        return result + odd
        
  • 其他解法

暂略。

相关文章

网友评论

      本文标题:66. LeetCode 409. 最长回文串

      本文链接:https://www.haomeiwen.com/subject/qmvbeqtx.html