题目描述:

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. C++中public、protected以及private的使用

    相比C语言,C++中通过class/struct来定义既包含数据,又包含行为的结构,从而支持了“对象”.现实世界中,一个人(一个对象)通常 拥有一些资产(数据),并且掌握某些技能(行为),并且这些资产 ...

  2. Django基础学习五_引入静态文件

    今天继续学习Django,今天主要掌握两个小点 一.如果为Django项目中引入静态文件 1.先要在project目录下创建static的目录,然后将jquery文件拷贝这个目录下就可以了 2.在pr ...

  3. 对于一个web工程,如果我们复制一个已有的工程粘贴到同一个workspace下,我们除了需要更改工程的名字还需要更改这个新工程的content root,否则会报错。

    对于一个web工程,如果我们复制一个已有的工程粘贴到同一个workspace下,我们除了需要更改工程的名字还需要更改这个新工程的content root,否则会报错.步骤如下: 右键新的工程---&g ...

  4. marioTcp

    https://github.com/nicholaszj/marioTcp MarioTCP MarioTCP 是使用libevent模型来建立的一个性能强大的TCP服务器. 1:Getting S ...

  5. Bitmap Images and Image Masks

    [Bitmap Images and Image Masks] Bitmap images and image masks are like any drawing primitive in Quar ...

  6. 基于httpcore(httpclient component)搭建轻量级http服务器

    下面是apache官网例子 服务器端接受请求,实现接收文件请求处理器 import java.io.File; import java.io.IOException; import java.io.I ...

  7. 作业一:博客和Github简单练习

    (1)自我介绍 Hello everybody! 我叫纪杨阳,学号1413042002,网络工程141班. 本人没啥特殊的兴趣爱好,都是些平常得不能再平常的吃吃睡睡.要说感兴趣的,可能就是音乐和服饰还 ...

  8. urlrewrite重写url(转)

    环境: Maven 3.0.4 Urlrewrite 2.5.2 Myeclipse 8.6.1 借此机会顺便提一下 Maven Project 的创建,会了的朋友或还不想了解 Maven 的朋友,可 ...

  9. [ruby]rubyGem出现ERROR: Could not find a valid gem时的处理方法

    场景: 想安装SASS的时候,打开cmd,输入gem install sass的时候却出现了: ERROR:  Could not find a valid gem 'sass' (>= 0), ...

  10. centos 安装vmware 9.02 报 Failed to load module "pk-gtk-module" "canberra-gtk-module"

    http://www.linuxidc.com/Linux/2012-01/50944.htm 系统平台:RHEL6.1 X86 32bit 软件版本:VMware-Workstation-Full- ...