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 ...
随机推荐
- 通过HttpWebRequest实现模拟登陆
1>通过HttpWebRequest模拟登陆 using System; using System.Collections.Generic; using System.Linq; using S ...
- 探究算子find_shape_model中参数MaxOverlap的准确意思
基于形状的模板查找算子: find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxO ...
- Python解决数独
Environment: Python27 # -*- coding: UTF-8 -*- ''' Created on 2017年6月9日 @author: LXu4 ''' import copy ...
- 牛掰的python与unix
python的中心哲学 Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on ...
- [Selenium] Java代码获取,设置屏幕分辨率
import java.awt.Dimension; import java.awt.DisplayMode; import java.awt.GraphicsDevice; import java. ...
- 1.oracle dblink(数据库不同实例数据对导)
.创建一个两个数据库之间的dblink,语法如下 create database link to_test connect to scott identified by tiger using '(D ...
- 关闭Found duplicated code
IDEA中的这个“发现重复代码 - Found duplicated code“的这个提示甚是烦躁. Settings —> Editor —> Inspections —> Gen ...
- xcconfig
[xcconfig] 1.When you can use a .xcconfig file? Use .xcconfig files if you find yourself changing th ...
- 马婕 2014MBA专硕考试 词汇每日一练(转)
2013-6-8 1. To ensure its sustained progress in economy, the government has _______ a series of poli ...
- cordova/webapp/html5 app 用corsswalk替换内核,优化安卓webview
Crosswalk与WebView的不同 为什么要用corsswalk?由于cordova应用在安卓上运行的时候,都是调用的手机webview,而在不同的安卓机.不同版本的系统上,webview的性能 ...