【LeetCode 409】 关关的刷题日记31Longest Palindrome

2017 年 11 月 9 日 专知 关关

点击上方“专知”关注获取更多AI知识!

关关的刷题日记31 – 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.

Example:

Input: "abccccdd"

Output: 7

Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.

题目要求找出一个字符串能够成的最大回文长度。

思路

思路:如果字符数目是偶数的话,那么该字符都可以加入构成回文。如果字符数目是奇数的话,那么只有偶数个该字符可以加入构成回文。还需要注意的是,如果出现了总共有奇数个的字符,最后可以选一个放在回文最中间位置。

class Solution {public:    int longestPalindrome(string s) {        map<char,int>m;        for(char x:s)        {            m[x]++;        }        int re=0, flag=0;        for(auto x:m)        {            if(x.second%2==0)                re+=x.second;            else            {                re+=x.second-1;                flag=1;            }        }        return re+flag;    }};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang,注明Leetcode刷题)。







专知网站查看Leetcode刷题日记:

请登录www.zhuanzhi.ai或者点击阅读原文,顶端搜索“Leetcode” 主题,取查看获得专知Leetcode所有资源!如下图所示~


群满,请扫描小助手,加入专知-LeetCode学习交流群,交流分享~


欢迎转发到你的微信群和朋友圈,分享专业AI知识!


获取更多关于机器学习以及人工智能知识资料,请访问www.zhuanzhi.ai,  或者点击阅读原文,即可得到!


-END-

欢迎使用专知

专知,一个新的认知方式!目前聚焦在人工智能领域为AI从业者提供专业可信的知识分发服务, 包括主题定制、主题链路、搜索发现等服务,帮你又好又快找到所需知识。


使用方法>>访问www.zhuanzhi.ai, 或点击文章下方“阅读原文”即可访问专知


中国科学院自动化研究所专知团队

@2017 专知


专 · 知


关注我们的公众号,获取最新关于专知以及人工智能的资讯、技术、算法、深度干货等内容。扫一扫下方关注我们的微信公众号。


点击“阅读原文”,使用专知

登录查看更多
4

相关内容

LeetCode is a social platform for preparing IT technical interviews.
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
160+阅读 · 2020年5月14日
《动手学深度学习》(Dive into Deep Learning)PyTorch实现
专知会员服务
115+阅读 · 2019年12月31日
【强化学习资源集合】Awesome Reinforcement Learning
专知会员服务
93+阅读 · 2019年12月23日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
关关的刷题日记90 – Leetcode 400. Nth Digit
专知
3+阅读 · 2018年1月8日
【关关的刷题日记60】Leetcode 437. Path Sum III
【 关关的刷题日记53】 Leetcode 100. Same Tree
专知
10+阅读 · 2017年12月1日
【 关关的刷题日记47】Leetcode 38. Count and Say
【LeetCode 136】 关关的刷题日记32 Single Number
【LeetCode 500】关关的刷题日记27 Keyboard Row
专知
3+阅读 · 2017年11月5日
Arxiv
3+阅读 · 2018年4月5日
VIP会员
Top
微信扫码咨询专知VIP会员