• Leetcode 409. 最长回文串(端午节快乐)


    在这里插入图片描述
    给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 。

    在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。

    示例 1:

    输入:s = "abccccdd"
    输出:7
    解释:
    我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
    

    示例 2:

    输入:s = "a"
    输入:1
    

    示例 3:

    输入:s = "bb"
    输入: 2
    

    提示:

    • 1 <= s.length <= 2000
    • s 只能由小写和/或大写英文字母组成

    主要思路:观察回文子串特性,基本是成对成对加一个奇数
    Code:
    class Solution {
    public:
    int longestPalindrome(string s) {
    map<char,int>mymap;

        for(int i=0;i<s.length();i++)
        {
            mymap[s[i]]++;
        }
        
        map<char,int>::iterator it;
        int res=0;
        bool flag=false;
        bool flag2=false;
        for(it=mymap.begin();it!=mymap.end();++it)
        {
            if(it->second%2==0)
                res+=it->second;
            else if(it->second==1)
            {
                flag=true;
            }
            else
            {
                flag2=true;
                res+=it->second-1;
            }
        }
        if(flag)
            res++;
        
        if(s.length()==1)
            return res;
        
        if(mymap.size()==1 && s.length()%2 )
            res++;
        if(flag2&&mymap.size()>=2&&!flag)
            res++;
        return res;
    }
    

    };

  • 相关阅读:
    浮动 无刷新 div
    vidalia 更换浏览器代理
    vb 主动 点击 a
    Delphi 资料收集
    用Linux的iptables和Python模拟广域网
    框架资源
    div 边框
    Google App Engine
    mac 雪豹 10.6 五国
    Windows Azure Storage (3) Windows Azure Storage Service存储服务之Blob详解(中)
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/16339817.html
Copyright © 2020-2023  润新知