LeetCode 409 Longest Palindrome
Problem:
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.
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.
Summary:
给出一个可能包含大写、小写字母的字符串,求用字符串中的字母可组成的最长回文串长度。此处大写、小写字母视为有区别。
Analysis:
1. 回文串包含两种形式:aba形式及aaa形式。在给出的字符串中,所有出现次数为偶数的字母都可以用为回文串中;若有出现次数为奇数的字母,则先将其包含的最大偶数次加入回文串长度中(即奇数次数减1),再在最后加上放在最中间的一个字母。下面为Hash表映射字母和出现次数的方法。
class Solution {
public:
int longestPalindrome(string s) {
int len = s.size(), ch[] = {}, res = ;
bool odd = false; for (int i = ; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
ch[s[i] - 'a']++;
}
else {
ch[s[i] - 'A' + ]++;
}
} for (int i = ; i < ; i++) {
if (ch[i] % == ) {
res += ch[i];
}
else {
odd = true;
res += ch[i] - ;
}
} return odd ? res + : res;
}
};
2. 如上已分析,得到的回文串长度为原字符串中所有出现偶数次的字母次数,以及出现奇数次的字母次数中的最大偶数,最终再加上回文串中间的一个字母(有奇数次字母时)。
这个结果相当于在原字符串中,将每一个出现奇数次的字母减掉一个,最终加上回文串中间的字母。下面的代码用count统计每个字母出现的次数,和1作&操作来判断是否为奇数。
class Solution {
public:
int longestPalindrome(string s) {
int odd = , len = s.size();
for (int i = ; i < ; i++) {
char c = i + 'a';
odd += count(s.begin(), s.end(), c) & ;
} for (int i = ; i < ; i++) {
char c = i + 'A';
odd += count(s.begin(), s.end(), c) & ;
} return odd ? len - odd + : len;
}
};
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 (最长回文)
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 ...
- 409. Longest Palindrome 最长对称串
[抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...
随机推荐
- 如何确定C#代码是在编译时执行还是在运行时执行
突然想起那个"switch..case..."的case标签都可以判断哪些类型... 就先搞了一个错误的demo... class Program { static void Ma ...
- VTK初学一,a Mesh from vtkImageData—球冠
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
一.测试环境介绍 1. 宿主使用工作站(HYPR-V)基本配置如下: 处理器:Intel(R) Core(TM) i5-4470 CPU @ 3.20GHz 3.20GHz 内存(RAM):8.00G ...
- JAVA-多屏幕显示
以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...
- Java多线程文件下载
一. 多线程下载文件考虑处理步骤: 1. 如何获取文件的长度 2. 合理的创建线程数量,并计算每一个线程下载的长度 3. 如何将多个线程下载的字节写入到文件中 二. 代码实现如下: package c ...
- leetcode 解题报告 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- uMlet建模工具
下载:http://www.umlet.com/ 无意中发现的一款建模工具,能快速搭建数据库模型,前置安装条件是java环境. 这是我建的user模型表,2个字段name和age,2个方法getAge ...
- VirtualBox CentOS安装增强功能与设置共享文件夹
如果安装的是CentOS minimal版无网络的可以看这篇文章. 一.安装依赖环境 依次执行如下命令 yum install update yum install kernel-headers yu ...
- String与InputStream互转的几种方法
[java] view plain copy /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @ ...
- 项目: 推送水木文章到Kindle
代码在github:https://github.com/Viyu/PushNewsmth2Mail 当年买Kindle的时候,想再Kindle上阅读水木的帖子,但Kindle的Web上网体验太差,想 ...