zoukankan      html  css  js  c++  java
  • leetcode 409. Longest Palindrome

    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.
    题目大意:
    给定一堆字母,求这些字母能能组合得到的最长的回文串的长度
    好久没做过这么简单的题目了,直接统计每种字符的个数,偶数的直接加,奇数的-1再求和,再加1。.

    class Solution {
    public:
        int longestPalindrome(string s) {
            map<char, int> mp;
            for (int i = 0; i < s.size(); ++i) {
                mp[s[i]]++;
            }
            int od = 0;
            int ev = 0;
            int mark = 0;
            for (auto x : mp) {
                if (x.second & 1) {
                    mark = 1;
                    od += x.second - 1;
                } else {
                    ev += x.second;
                }
            }
            if (mark) od++;
            return ev + od;
        }
    };
    
  • 相关阅读:
    uva400 Unix ls
    cf641 div2 abcd
    cf619 div2 abcd
    cf620 div2 abcde
    atc160
    cf638 div2 abcd
    CodeCraft-20(Div. 2 abcd
    cf Round 621 abcd
    luogu1941 飞扬的小鸟
    UVA1601 The Morning afther Halloween
  • 原文地址:https://www.cnblogs.com/pk28/p/7520007.html
Copyright © 2011-2022 走看看