leetCode题解之Longest Palindrome

1、题目描述

2、问题分析

直接用hash table 做就行。

3、代码

 1 int longestPalindrome(string s) {
 2         if(s.size() == 0)
 3             return 0;
 4         map<char,int> m;
 5         for(auto &c : s)
 6             m[c]++;
 7         
 8         int ans = 0;
 9         int odd = 0;
10         for(auto p = m.begin(); p != m.end() ; ++p ){
11             if( p->second % 2 == 0)
12                 ans += p->second;
13             else{
14                 ans += p->second-1;
15                 odd++;
16             }
17                 
18         }
19         if (odd != 0)
20             ans  += 1;
21         return ans;
22         
23     }
pp
原文地址:https://www.cnblogs.com/wangxiaoyong/p/9470637.html