题目:

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.

分析:

给定一个字符串,用其所有的字符,求能拼成的最大回文串长度。

统计所有字符出现的频率,计算字符成对的数量,因为只有两个字符才可以组成回文串,当然,如果字符为奇数的话,意味着我们可以将这个字符放在字符串中间来构成回文串。最后数量加和即可。

程序:

C++

class Solution {
public:
int longestPalindrome(string s) {
if(s == "")
return 0;
vector<int> a(128, 0);
for(char &c:s)
a[c]++;
int odd = 0;
int res = 0;
for(const int num:a){
res += (num >> 1) << 1;
if(num & 1)
odd = 1;
}
return res + odd;
}
};

Java

class Solution {
public int longestPalindrome(String s) {
if(s.length() == 0)
return 0;
int[] a = new int[128];
for(int i = 0; i < s.length(); ++i){
a[s.charAt(i)]++;
}
int res = 0;
int odd = 0;
for(final int num:a){
res += (num >> 1) << 1;
if((num & 1) == 1)
odd = 1;
}
return res + odd;
}
}

LeetCode 409. Longest Palindrome 最长回文串(C++/Java)的更多相关文章

  1. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. 409 Longest Palindrome 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...

  3. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. Longest Palindrome 最长回文串问题

    1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  5. 算法笔记_032:最长回文串(Java)

    目录 1 问题描述 2 解决方案 2.1 中心扩展法 2.2 Manacher算法   1 问题描述 给定一个字符串,求它的最长回文子串的长度. 2 解决方案 2.1 中心扩展法 此处,首先枚举出回文 ...

  6. C#LeetCode刷题之#409-最长回文串(Longest Palindrome)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...

  7. leetcode 5 Longest Palindromic Substring--最长回文字符串

    问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  8. LeetCode409Longest Palindrome最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  9. POJ----(3974 )Palindrome [最长回文串]

    Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 1834 Description Andy ...

  10. Java实现 LeetCode 409 最长回文串

    409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...

随机推荐

  1. drf视图类的继承关系

  2. kratos http原理

    概念 kratos 为了使http协议的逻辑代码和grpc的逻辑代码使用同一份,选择了基于protobuf的IDL文件使用proto插件生成辅助代码的方式. protoc http插件的地址为:htt ...

  3. 第九課-Channel Study For Caller Client Api

    1.浏览Mirth Connect开放Client API 2.启动Vs2019,编程测试Client Api的调用 开启你在宇宙第一IDE下熟悉的.net之旅吧!

  4. 第四課-Channel Study File Reader & File Writer

    示例描述:从数据库中读取数据并过滤转换为HL7并存放到指定目录;然后读取目录中的HL7文件转换为txt文本并存放到指定目录. 首先在F:\MirthConnect\Test目录下创建Out目录存放输出 ...

  5. 力扣523(java&python)-连续的子数组和(中等)

    题目: 给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组: 子数组大小 至少为 2 ,且子数组元素总和为 k 的倍数.如果存在,返回 true ...

  6. Serverless 架构落地实践及案例解析

    简介: 技术演进的本质是更好服务业务,传统开发方式使企业花费更多的精力打磨底层技术细节,而 Serverless 架构就是让开发者专注业务实现从而创造更大的业务价值. 作者 | 丹坤   整理 | 徐 ...

  7. dotnet 谨慎在静态构造函数里使用锁

    在 dotnet 的最佳实践里面,不推荐在静态构造函数里面包含复杂的逻辑,其中也就包含了本文聊的和多线程相关的锁的使用.最佳做法是尽量不要在静态构造函数里面碰到任何和锁以及多线程安全相关的逻辑.本文来 ...

  8. Quartus prime 的安装步骤:

  9. vue实现左右两列竖直分别滑动,且双向关联的选项卡(二)

    查了诸如vant,mint组件上并没有找到期望的这种效果(cube组件上有,但项目中实在不想再引入一个第三方的组件库了),但实际上在移动端app开发中很常见的一个效果.于是按照自己的思路将这个效果做了 ...

  10. Linux服务器安装GaussDB 100及安装过程中常见问题解决

    ******************************** Gaussdb 100安装 ******************************** 1. 创建安装包目录 mkdir -p ...