亚麻:Longest Palindrome

假如想象 提交于 2020-01-04 14:57:26

亚麻 的OA题

代码在leetcode 测试通过; leetcode 链接

https://leetcode.com/problems/longest-palindrome/description/

  

class Solution {
public:
    
    
    int longestPalindrome(string s) {
        if( s.empty () ) return 0;
      
        unordered_map< char, int > res;
        int max =0;
        bool oflag = false;
        
        for (auto& e: s)
            res[e]+=1;
        
        for (auto& e: res){
            if (e.second%2 == 0)
                max += e.second;
            else {
                oflag = true ;
                max += e.second -1;
            }
        }
        if(oflag)
            return max +1;
        else 
            return max;
    }
};

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!