LeetCode 409. Longest Palindrome 题解(C++)
题目描述
- 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.
示例
Input:
“abccccdd”Output:
7Explanation:
One longest palindrome that can be built is “dccaccd”, whose length is 7.
思路
- 设置一个由52个由bool类型的元素组成的数组,用来记录字符串s中出现的每个字符的次数,比如出现a,则对应数组下标为0里的数值加1,若是A,则在数组的下标为26;这里由ASCII码同英文字母大小写相隔32,且A在ASCII码的前面(比如A的ASCII码十进制表示为65,a的ASCII码十进制表示为97),所以若是大写字母,则需要加上58(大小写相隔的32加上前26个属于小写的位置);
- 遍历字符串s,若该字符相对应的数组元素为false,则将该元素变为true,若是true,则表示