原题:
这个就是对字符串里面的字符进行排序,顺便检测一下有没有落单的字符。
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.
代码如下:
int longestPalindrome(char* s) {
if(strlen(s)==1)
return 1;
if(strlen(s)==0)
return 0;
int cmp(const void* a,const void* b)
{
return *(char*)a-*(char*)b;
}
qsort(s,strlen(s),sizeof(char),cmp);
int flag=0;
int sum=1;
int result=0;
//printf("%s",s);
for(int n=1;n<strlen(s);n++)
{
//printf("%c,%c.",*(s+n),*(s+n-1));
if(*(s+n)==*(s+n-1))
{
sum++;
}
else
{
if(sum%2==0)
{
result+=sum;
}
else
{
sum-=1;
result+=sum;
flag=1;
}
sum=1;
}
}
if(sum%2==0)
result+=sum;
else
{
result+=(sum-1);
flag=1;
}
if(flag==1)
result++;
return result;
}
这个就是对字符串里面的字符进行排序,顺便检测一下有没有落单的字符。
顺序统计就好。
最近觉得用c写一些库可能会有些用。