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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步