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.
要完成的函数:
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(统计字母出现次数)的更多相关文章
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode 409. Longest Palindrome (最长回文)
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode——409. Longest Palindrome
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- Sprint boot notes
1. spring.io 官网 2. http://javainuse.com/spring/sprboot spring boot学习资源 3. spring boot websocketss视频 ...
- post传参
1.form表单 2.数据流
- 20155317 2016-2017-2 《Java程序设计》第7周学习总结
20155317 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 1.在只有Lambda表达式的情况下,参数的类型必须写出来. 2.Lambda表达式本身是中 ...
- tips 移出 消失和 移入 显示
//规则tipsvar tip_index = 0;$(document).on('mouseenter', '.layui-badge-rim', function(){ tip_index=lay ...
- PV对第三方存储的访问模式支持
访问模式 PV可以使用存储资源提供商支持的任何方法来映射到host中.如下的表格中所示,提供商有着不同的功能,每个PV的访问模式被设置为卷支持的指定模式.比如,NFS可以支持多个读/写的客户端,但可以 ...
- 8-5 Navicat工具与pymysql模块
一 Navicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 需要掌握的基本操作 ...
- UIPickerView 修改必须滚动才修改值的bug
//相应的选择转动 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSI ...
- .NET基础 (17)反射
反射1 请解释反射的基本原理和其实现的基石2 .NET提供了哪些类型来实现反射3 如何实现动态地发射程序集4 如何利用反射来实现工厂模式 反射1 请解释反射的基本原理和其实现的基石 反射是一种动态分析 ...
- Ubuntu 14.04 install emacs 24.5
1.前期准备工作 2.安装基础构件工具 3.下载emacs编译需要的依赖库 4.下载emacs24.5编译安装 5.下载并安装我的emacs配置文件 6.配置tmux和zsh 1. 前期准备工作 在阿 ...
- [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域
变量的作用域 一个变量的作用域(scope)是程序中定义这个变量的区域. 全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义. 而在函数之内 ...