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 ...
随机推荐
- MySQL 根据年、季度、月、周、日统计数据
-- 计算每年订单的总价格 select date_format(t.order_time,'%Y') years,sum(t.order_amount) '总价格' from lf_order t ...
- spring定时任务执行两次的原因与解决方法
spring定时任务,本地执行一次,放到服务器上后,每次执行时会执行两次,原因及解决办法. http://blog.csdn.net/yaobengen/article/details/7031266 ...
- Appium自动化测试-iOS
Appium的哲学 我们相信,对原生应用的自动化测试,应当不必要包含其他的SDK组件或者特别编译您的App,并且应当可以选择任何您喜欢的测试方法,框架和工具.基于这些出发点我们开发了Appium.现在 ...
- HDU 5117 Fluorescent (数学+状压DP)
题意:有 n 个灯,初始状态都是关闭,有m个开关,每个开关都控制若干个.问在m个开关按下与否的2^m的情况中,求每种情况下亮灯数量的立方和. 析:首先,如果直接做的话,时间复杂度无法接受,所以要对其进 ...
- 一)get started with the Quartz project
官网 http://www.quartz-scheduler.org/ 下载链接 http://www.terracotta.org/download/reflector.jsp?b=tcdistri ...
- Linux 基础教程 42-xargs命令
xargs是execute arguments的缩写,主要作用是从标准输入中读取内容,并将此内容传递给它要协助的命令,并作为要协助命令的参数来执行. 基本语法 xargs [选项] [命令] ...
- (博弈 sg入门)kiki's game -- hdu -- 2147
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2147 题意: 在一个n*m的棋盘上,从 (1,m),即右上角开始向左下角走. 下棋者只能往左边(lef ...
- Android-Activity启动模式(launchMode)
Activity启动模式是非常重要的一块内容,启动模式直接关系到用户的体验 和 性能的提升等 Activity启动模式分为四种: 如果不配置:launchMode,默认就是:standard 标准的 ...
- 词频统计-part2
看到这个问题为之一愣,这简单多了,在第一部分的基础上把那些存储结构删了,把排序算法删了,设置一个变量,遇到则加一,直到读到文件尾.最后输出单词出现次数. 程序比较简单也比较,下面就把程序贴出来: pa ...
- UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
Scrapy爬虫向数据库写入数据时报错: UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ord ...