首页 > 其他 > 详细

409. Longest Palindrome

时间:2016-10-25 14:32:50      阅读:234      评论:0      收藏:0      [点我收藏+]

 感觉需要注意的点就是在A-Z和a-z之间还夹了6个其他的字符,所以统计的时候不能用new int[52]需要new int[58]

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.

代码;

 1     public int longestPalindrome(String s) {
 2         if(s == null) {
 3             return 0;
 4         }
 5         int[] count = new int[60];
 6         for(int i = 0; i < s.length(); i++) {
 7             count[s.charAt(i) - ‘A‘]++;
 8         }
 9         boolean hasOdd = false;
10         int res = 0;
11         for(int i = 0; i < 60; i++) {
12             if(count[i] % 2 == 0) {
13                 res += count[i];
14             } else {
15                 res += count[i] - 1;
16                 hasOdd = true;
17             }
18         }
19         res += hasOdd? 1: 0;
20         return res;
21     }

 

409. Longest Palindrome

原文:http://www.cnblogs.com/warmland/p/5996353.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!