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 ...
随机推荐
- 包介绍 - Fluent Validation (用于验证)
Install-Package FluentValidation 如果你使用MVC5 可以使用下面的包 Install-Package FluentValidation.MVC5 例子: public ...
- 关于COOKIE学习的一二
index.php <?php setcookie("name","dalisng",time()+3600); setcookie("addr ...
- 使用 Flexbox 的居中布局
- 如何给wordpress外部链接自动添加nofollow
wordpress多作者博客可以丰富网站的内容,但同时也会产生一些无关的链接,例如有些投机的人会考虑在文章中随意添加外部链接,如果你不想给这些外部链接传递权重,你需要给这些外部链接加上 rel=&qu ...
- 读取XML文件
首先要确定好XML文件的位置,最好是放在程序的debug文件中,放在其他地方也可以,要写上绝对路径 using System; using System.Collections.Generic; us ...
- lianjie
数值策划入门:如何确定游戏中的资源价值和定价http://bbs.gameres.com/thread_494366.html 一张常规的RPG游戏地图的制作流程 http://bbs.gameres ...
- Redis学习笔记八:独立功能之二进制位数组
Redis 提供了 setbit.getbit.bitcount.bitop 四个命令用于处理二进制位数组. setbit 命令用于为位数组指定偏移量上的二进制位设置值,偏移量从 0 开始计数. ge ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- 28 GroupSock(NetAddress)——live555源码阅读(四)网络
28 GroupSock(NetAddress)——live555源码阅读(四)网络 28 GroupSock(NetAddress)——live555源码阅读(四)网络 简介 1) NetAddre ...
- 网络数据包发送工具PacketSender中文源码
在网上发现了一个好用的工具PacketSender,数据包发送器.对于写网络程序来说,有很大的便利性.虽然在linux下,netcat工具也很好用,但是这个也不错. 原本是英文的,给翻译了一下.这是基 ...