题目:

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. Pygame安装以及解决问题:Try to run this command from the system terminal. Make sure that you use the correct version of 'pip......

    在这里记录一下我的安装过程: 1.首先找到自己python程序安装目录下的Scripts文件夹(里面有pip这里面): 2.使用快捷键win + R 打开终端,先进入到安装python的盘符,然后进入 ...

  2. OpenYurt:延伸原生 Kubernetes 到边缘场景下的落地实践

    简介: 随着云原生技术的逐步成熟,阿里云容器服务团队在具体落地实践过程中不断探索云原生技术的应用边界.同时随着物联网和 5G 的迅猛发展,传统的边缘计算架构已经不能满足业务发展的需要. 如何基于云原生 ...

  3. [Contract] truffle-flattener 合并 Solidity 文件的依赖到一个文件

    使用 $ npm install truffle-flattener -g $ truffle-flattener <solidity-files> > output.sol 为什么 ...

  4. RT-Thread 运行时常见错误

    一.空线程栈较小 现象: 现象一: 现象二: 原因: 从RT-Thread文章中心可知,空闲线程是不能被挂起的,官方文档说明如下图所示: 注意:必须保证空闲线程的栈空间足够,否则空闲线程内存溢出后,也 ...

  5. 习题8 #第8章 Verilog有限状态机设计-2 #Verilog #Quartus #modelsim

    2. 设计一个"1001"串行数据检测器,其输入.输出如下: 输入x:000 101 010 010 011 101 001 110 101 输出z:000 000 000 010 ...

  6. Google高精度的搜索技巧

    利用"关键字",完全匹配搜索(双引号精准搜索). 利用"关键字:档案类型",搜寻特定档案类型. 例如:"简历:doc"."钢铁侠: ...

  7. docker-compose 安装 etcd

    目录 docker-compose.yaml docker-compose.yaml version: "3" services: etcd: hostname: etcd ima ...

  8. 05 elasticsearch学习笔记-基本CRUD

    目录 视频教程 4.1 基本CRUD 4.2 URI查询 按时间段查 视频教程 Elasticsearch(7.8.1)沥血之作(包含仿百度搜索案例) https://www.bilibili.com ...

  9. NASM中的ALIGN ALIGNB SECTALIGN

    ALIGN与ALIGNB NASM中的ALIGN与ALIGNB是用来字节对齐的,它们接收2个参数,第一个参数是必须的,表示对齐的字节数(必须是2的幂),第二个参数是可选的,表示为了对齐而进行填充的内容 ...

  10. LVS负载均衡(6)--LVS调度算法详解

    目录 1. LVS调度算法详解 1.1 静态调度算法 1.1.1 RR调度算法 1.1.2 WRR调度算法 1.1.3 SH调度算法 1.1.4 DH调度算法 1.2 动态调度算法 1.2.1 LC调 ...