原题:
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.
给出一串包含大小字母的字符串计算最大可以组合成多长的回文串,区分大小写。哈希表就行,最后把哈希表中56个元素全部除以2加起来,如果其中有余1的最后再加1作为中心元素就行,点了提交,然后leetcode炸了,让我一度怀疑是不是我的代码问题,哈哈哈结果:
Success
Runtime: 4 ms, faster than 92.36% of C++ online submissions for Longest Palindrome.
Memory Usage: 8.6 MB, less than 95.19% of C++ online submissions for Longest Palindrome.
代码:
class Solution {
public:
int longestPalindrome(string s) {
int i=0;
int lib[52]={0};
while(i<s.size()){
if(s[i]>'@'&&s[i]<'['){lib[s[i]-'A']++;}
else{lib[s[i]-'a'+26]++;}
i++;
}
bool single=false;
int re=0;
for(i=0;i<52;i++){
re+=lib[i]/2;
if(!single&&lib[i]%2==1){single=true;}
}
return 2*re+single;
}
};