LeetCode 409. Longest Palindrome 最长回文串(C++/Java)
题目:
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)的更多相关文章
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- Longest Palindrome 最长回文串问题
1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
- 算法笔记_032:最长回文串(Java)
目录 1 问题描述 2 解决方案 2.1 中心扩展法 2.2 Manacher算法 1 问题描述 给定一个字符串,求它的最长回文子串的长度. 2 解决方案 2.1 中心扩展法 此处,首先枚举出回文 ...
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- LeetCode409Longest Palindrome最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...
- POJ----(3974 )Palindrome [最长回文串]
Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 5121 Accepted: 1834 Description Andy ...
- Java实现 LeetCode 409 最长回文串
409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...
随机推荐
- 修改中文、英文参考文献在文末列表中的顺序:EndNote
本文介绍在EndNote软件中,使得参考文献按照语种排列,中文在前.英文在后的方法. 前期我们在EndNote参考文献格式Output Styles界面介绍一文中,详细介绍了文献管理软件End ...
- js 连接数据库 提示:ActiveXObject is not defined
ActiveXObject is not defined 最近比较闲,上班瞎捣鼓一下,没想到报错了,提示ActiveXObject is not defined 大概是在js连接数据库时new对象使用 ...
- 力扣907(java)-子数组的最小值之和(中等)
题目: 给定一个整数数组 arr,找到 min(b) 的总和,其中 b 的范围为 arr 的每个(连续)子数组. 由于答案可能很大,因此 返回答案模 10^9 + 7 . 示例 1: 输入:arr = ...
- 基于SLS构建RDS审计合规监控
简介: 数据库是企业业务的数据核心,其安全方面的问题在传统环境中已经成为泄漏和被篡改的重要根源.因此,对数据库的操作行为尤其是全量 SQL 执行记录的审计日志,就显得尤为重要. 背景 数据库是企业业务 ...
- 阿里云AHAS Chaos:应用及业务高可用提升工具平台之故障演练
简介: 阿里云AHAS Chaos:应用及业务高可用提升工具平台之故障演练 应用高可用服务AHAS及故障演练AHAS Chaos 应用高可用服务(Application High Availabili ...
- Flink Sort-Shuffle 实现简介
简介:Sort-Shuffle 使 Flink 在应对大规模批数据处理任务时更加游刃有余 本文介绍 Sort-Shuffle 如何帮助 Flink 在应对大规模批数据处理任务时更加游刃有余.主要内容包 ...
- 阿里云 EventBridge 事件驱动架构实践
简介:我们认为 EventBridge 是云原生时代新的计算驱动力,这些数据可以驱动云的计算能力,创造更多业务价值. 作者:周新宇 本文内容整理自 中国开源年会 演讲 首先做一个自我介绍,我是 Ro ...
- [Trading] 专业交易: 专业交易员和散户交易员的不同, 什么是专业交易员
专业交易员可能用的是公司的钱或者自己的钱 有基本工资支持,散户用的是自己的钱 没有人提供工资来做交易. 目标不同,专业交易员的目的是增长投资账户和获得奖金,散户大部分是为了提取盈利收入而无法增长投资账 ...
- Ubuntu 20.04版本安装k8s控制节点与控制节点升级
一.环境配置 服务器配置:2核4G IP:192.168.10.23 主机名:master4将改主机加入此 集群 # 1.修改主机名 hostnamectl set-hostname master4 ...
- ruby操作excel
操作xlsx axlsx插件 操作xls spreadsheet插件