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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

CGI 程序设计自学通

CGI 程序设计自学通

(美)格里高利 / 徐丹/等 / 机械工业出版社 / 1998-08 / 28.00元

本书集中讨论CGI编程,以便利用一起来看看 《CGI 程序设计自学通》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具