LeetCode——409. Longest Palindrome
题目:
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: Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
大意:
给出一个由小写或大写字母组成的字符串,找到能被其中的字母组成的最长的回文的长度。
这是区分大小写的,比如“Aa”就不能认为是回文。
注意:
假设给出的字符串长度不会超过1010。
例子:
输入:
“abccccdd” 输出: 解释:
最长的回文为“dccaccd”,长度为7。
回文就是中间有一个单个的字母,两边的字母是对称的。
- aaadd的最长回文是daaad
- aaadddcc的最长回文是cdaaadc或者cadddac
可以总结为:
- 数目为偶数的字母将全部出现在最长回文中,
- 奇数就有两种情况,有没有大于1个的,如果有大于1个的。就比如有3个,那么回文长度将加上2。如果有7个就加上6.
- 如果所有的奇数都不大于1,最后结果再加1即可
class Solution {
public int longestPalindrome(String s) {
int result = 0;
boolean flag = false;
int[] a = new int[26*2];
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) < 'a') {
a[s.charAt(i) - 'A' + 26]++;
} else {
a[s.charAt(i) - 'a'] ++;
}
}
for(int i:a){
if(i == 1){
flag = true;
}
if(i > 1){
result += i / 2 * 2;
if (i % 2 == 1) flag = true;
}
}
if(flag){
result++;
}
return result;
}
}
这里使用一个26*2的数组记录字幕个数,因为是区分大小写的,每次如果字母数目大于1先除2再乘2,这样偶数不变,奇数将减一。还设置了一个flag变量判断是否存在奇数。
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
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- 【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 ...
随机推荐
- 致远A8任意文件写入漏洞_getshell_exp
近期爆出致远 OA 系统的一些版本存在任意文件写入漏洞,远程攻击者在无需登录的情况下可通过向 URL /seeyon/htmlofficeservlet POST 精心构造的数据即可向目标服务器写入任 ...
- elasticsearch 集群部署,版本 5.5.0
准备说明: 两台服务器 Ip分别为 192.168.239.78(主),192.168.239.49(从) 主服务器上配置如下:1.上传es5.5.0版本至主服务器2.解压 unzipunzip el ...
- async与await详解
async和await只是编译器功能.编译器会用Task类创建代码.如果不适用这两个关键字,也可以用C#4.0和Task类实现同样的功能,只是没有那么方便. 题主在概念上确实混淆的不行,但是确实asy ...
- 组件--button详解
一.wxss尺寸单位rpx rpx(responsive pixel): 可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx. 严格按照XML语法. 二.icon 图标组件 <!--index ...
- Latch设计模式
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; public class Te ...
- wordpress备份和还原和迁移
备份用mysqldump -u root -p test person > backup.sql 还原用mysql -u root -p < ./backup.sql 数据库密码修改后怎么 ...
- cocos creator 小游戏区域截图功能实现
截图是游戏中非常常见的一个功能,在cocos中可以通过摄像机和 RenderTexture 可以快速实现一个截图功能,具体API可参考:https://docs.cocos.com/creator/m ...
- JS+Jquery自定义格式导出HTML为Word(下列插件同样可以用于Excel导出)
这里的word导出主要采用了jquery.wordexport.js.FileSaver.js,做功能之前我也是找了很多网上的资料,里面涉及到js导出word的用的都是这个插件,只是在自定义样式这一块 ...
- Android入门简介
GeoQuiz应用是由一个activity和一个布局(layout)组成. activity是Android SDK中Activity类的一个具体实例,负责管理用户与信息屏的交互. 布局定义了一系列用 ...
- 《VR入门系列教程》之3---运动追踪与输入设备
运动追踪设备 第二种可以使人脑相信它真实处于虚拟世界的关键技术就是运动追踪技术,它可以通过追踪头部的运动状态实时更新渲染的场景.这与我们在真实世界中观看周围非常类似. 高速的惯性测量单元( ...