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

409. 最长回文串

作者: 上杉丶零 | 来源:发表于2020-03-19 17:38 被阅读0次
class Solution {
    public int longestPalindrome(String s) {
        if (s == null || "".equals(s)) {
            return 0;
        }

        int[] counts = new int[75];

        for (char c : s.toCharArray()) {
            counts[c - '0']++;
        }

        int length = 0;
        boolean hasOdd = false;

        for (int count : counts) {
            if (count == 0) {
                continue;
            }

            length += count / 2 * 2;

            if (!hasOdd && count % 2 == 1) {
                hasOdd = true;
            }
        }

        if (hasOdd) {
            length += 1;
        }

        return length;
    }
}
image.png

相关文章

  • 409.最长回文串

    409. 最长回文串[https://leetcode.cn/problems/longest-palindrom...

  • 关于回文问题

    回文问题的解法:双指针,栈,reverse 1. 409. 最长回文串[✔]2. 125. 验证回文串[✔]3. ...

  • 手撕LeetCode #409——Python

    409. 最长回文串[https://leetcode-cn.com/problems/longest-palin...

  • Leetcode 409 最长回文串

    409. 最长回文串[https://leetcode-cn.com/problems/longest-palin...

  • Leetcode409最长回文串--Python3

    409. 最长回文串 难度简单 解答: 语言:python 用字典进行统计长度,回文字符串的长度= 偶数个字符长度...

  • 409. 最长回文串

    题目描述 409. 最长回文串 思路 题目不难,就是所有的坑我都踩进去了。"abccccdd" -> a: 1, ...

  • 409. 最长回文串

    内容 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小...

  • 409. 最长回文串

    题目 我刚开始的想法 因为题目要求的是输出长度,并不需要将最长的回文字串数出来,所以很显然是一道找规律的题目,回文...

  • 409. 最长回文串

    解题思路 既然是求最长回文串,那么左右字符个数应该对称。对于偶数个字符来说,正好可以左右排列;而对于奇数个字符来说...

  • 409. 最长回文串

网友评论

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

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