题目描述:

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.

要完成的函数:

int longestPalindrome(string s)

说明:

1、这道题给定一个字符串,要求用字符串中的元素(包含大写字母和小写字母)组成一个尽可能长的回文串,最后返回这个回文串的长度。

比如字符串为"abccccdd",那么我们有两个d,四个c,一个b,一个a,所以我们可以组成一个最长的回文串是“dccaccd”,长度为7。

注意"Aa"在这道题目中,不被认为是回文串,也就是大小写敏感。

2、所以这道题我们统计一下有多少个偶数个数的字母,用长度为26*2=52的vector存储字母的出现次数。

出现一对偶数个数的字母的时候,结果+2。

最后再看一下有没有单个的字母,如果有,就加1,如果没有,那么结果不改变。

代码如下:(附详解)

    int longestPalindrome(string s)
{
vector<int>lettercount(52,0);//存放26个小写字母和26个大写字母
int result=0,t1,t2;//t1和t2是临时变量
for(char a:s)//我发现这种写法比传统的int i=0;i<s.size();i++方便很多
{
if(islower(a))//大小写分开处理
{
t1=a-'a';
if(lettercount[t1]==1)//如果之前已经出现过了
{
result+=2;
lettercount[t1]=0;
}
else//如果之前没有出现过
lettercount[t1]=1;
}
else//大小写分开处理
{
t2=a-'A'+26;
if(lettercount[t2]==1)
{
result+=2;
lettercount[t2]=0;
}
else
lettercount[t2]=1;
}
}
for(int i:lettercount)//最后遍历一遍52个元素,看有没有单个的元素
{
if(i==1)
{
result++;
break;
}
}
return result;
}

上述代码实测6ms,beats 98.02% of cpp submissions。

leetcode-409-Longest Palindrome(统计字母出现次数)的更多相关文章

  1. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  2. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  3. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  6. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  7. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  8. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

随机推荐

  1. 博客搬家到blog.wu8685.com

    博客园算是我最开始来的地方了吧,当时还在学校,为了找工作会看一些理论方面的东西,所以写的都是偏理论的心得. 后来参加了工作,开始忙起来,也就没有时间来更新了.其实忙都是借口,这点还是需要反思的. 大概 ...

  2. PHP 中 Traits 的简单使用

    PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西.       Traits可以理解 ...

  3. [Selenium]How to click on a hidden link ,move to the drop down menu and click submenu

    <table id="_paid_19" class="GOMainTable" cellspacing="0" cellpaddin ...

  4. PHP5.2 $arr = [] 初始化数组出现问题

    初始化数组  $arr=[] ,出现问题,使用 $arr = array() ,一切正常

  5. Oracle学习笔记(六)

    八.函数 1.函数的作用 (1)方便数据的统计 (2)处理查询结果,让数据显示更清楚 2.函数分类(提供很多内置函数,也可自定义函数) (1)数值函数 平均值,四舍五入 a.四舍五入 表达式 roun ...

  6. Selenium Webdriver定位元素的几种方式

    原文:http://www.cnblogs.com/tobecrazy/p/4570494.html 工作中使用到记录一下. 主要有: 上传 alter dialog prompt dialog co ...

  7. Java 代理模式(二) Java中的动态代理

    动态代理类 Java动态代理类位于java.lang.reflect包下,一般主要涉及到以下两个类: 1.Interface InvocationHandler 该接口中仅定义了一个方法: Objec ...

  8. php的循环与引用的一个坑

    上代码 $arr = array( 'a'=> 'a11', 'b'=> 'b22', 'c'=> 'c33', ); foreach ($arr as $k=>&$v ...

  9. java项目 远程debug

    AVA项目无法像PHP那样可以随时修改文件内容进行调试,调试可以借助eclipse,本地代码的话很容易在本地debug,但如果代码已经打包部署在linux上呢?可以进行远程debug   很简单,只需 ...

  10. unity 移动物体的方式

    1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...