409. Longest Palindrome

栏目: Java · 发布时间: 5年前

内容简介:Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not considered a palindrome here.Note:

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:

Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

难度:easy

题目:给定包含大小写字符组成的字符串,找出用这些字符串所能够成的最长回文串。

思路:字符统计

Runtime: 5 ms, faster than 90.61% of Java online submissions for Longest Palindrome.

Memory Usage: 34.7 MB, less than 100.00% of Java online submissions for Longest Palindrome.

class Solution {
    public int longestPalindrome(String s) {
        int[] table = new int[255];
        for (char c: s.toCharArray()) {
            table[c]++;
        }
        int length = 0, odd = 0;
        for (int i = 0; i < 255; i++) {
            if (table[i] % 2 > 0) {
                odd = 1;
            }
            
            length += table[i] - table[i] % 2;
        }
        
        return length + odd;
    }
}

以上所述就是小编给大家介绍的《409. Longest Palindrome》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP实战

PHP实战

Dagfinn Reiersol、Marcus Baker、Chris Shiflett / 张颖 等、段大为 审校 / 人民邮电出版社 / 2010-01 / 69.00元

“对于那些想要在PHP方面更进一步的开发者而言,此书必不可少。” ——Gabriel Malkas, Developpez.com “简而言之,这是我所读过的关于面向对象编程和PHP最好的图书。……强烈推荐此书,绝不要错过!” ——Amazon评论 “此书是理论与实践的完美融合,到目前为止,其他任何图书都无法与它相媲美。如果5颗星是满分,它完全值得10颗星!” ——A......一起来看看 《PHP实战》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换