标签:foreach bool longest code cti pre new rom href
public static int LongestPalindrome(string s) {
int length = 0;
Dictionary<char, int> dictionary = new Dictionary<char, int>();
int value = 0;
foreach (char c in s) {
if (dictionary.TryGetValue(c, out value)) {
dictionary[c]+=1;
} else {
dictionary[c] = 1;
}
}
bool addCenter = false;
foreach (int val in dictionary.Values) {
if (addCenter == false && val % 2 != 0) {
length++;
addCenter = true;
}
if (val > 1) {
length += (val - val % 2);
}
}
return length;
}
409.求最长回文串的长度 LongestPalindrome
标签:foreach bool longest code cti pre new rom href
原文地址:http://www.cnblogs.com/xiejunzhao/p/6cffae3adffdcf84c9fd6b087d900361.html