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:
7 Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
题目的大体意思:
给定一个字符串包含大小写,用字符串中的字符组成一个回文串求符合要求的回文串的最大长度。
利用HashSet中去重复的特性,出现偶数重复的的话,就从set中remove出去,出现一次或者奇数次数的话,就会添加到HashSet中,根据String中总长度和set的长度判断回文的长度。
public int longestPalindrome(String s) {
if(s==null|| s.length()==0)
return 0;
if(s.length()==1)
return 1;
HashSet<Character> set=new HashSet<Character>();
for(int i=0;i<s.length();i++){
if(set.contains(s.charAt(i))) {
//如果重复就
set.remove(s.charAt(i));
} else {
set.add(s.charAt(i));
}
}
if(set.size()>0)
return s.length()-set.size()+1;
else
return s.length()-set.size();
}
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
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- [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 longes ...
- 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 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
随机推荐
- SSD固态硬盘的闪存芯片颗粒介绍
固态硬盘凭借其存取速率超快等自身优势,被越来越多的电脑爱好者所青睐,并迅速普及到了广大用户的电脑中,因为固态硬盘与传统机械硬盘相比,确实在运行效率等方面有了质的提升,这里先了解一些评判固态硬盘优劣的知 ...
- windows系统激活-使用微软官方公布的kms client setup key安装或安装后使用slmgr导入
windows 10各版本: Windows 10 Professional W269N-WFGWX-YVC9B-4J6C9-T83GX Windows 10 Professional N MH37W ...
- iOS黑客技术相关
在黑客的世界里,没有坚不可破的防护系统,也没有无往不胜.所向披靡的入侵利器,有时候看似简单的问题,破解起来也许花上好几天.好几个月,有时候看似很 low 的工具往往能解决大问题:我们以实现微信自动抢红 ...
- nhibernate 配置nvarchar(max)
若你真的需要一个nvarchar(max)的sql存储空间时,记得增加 .CustomType("StringClob") Demo:Map(x => x.ContentM ...
- ORA-29538、ORA-29532、ORA-29913问题解决
问题一:ERROR at line 1: ORA-29538: Java not installed解决方法1.检查有没有安装JAVA组件select * from v$option t where ...
- 【PowerOJ1739】 魔术球问题
https://www.oj.swust.edu.cn/problem/show/1739 (题目链接) 题意 n个柱子上放小球,每根柱子上相邻两个小球的数字之和必须是完全平方数,只有放了x号小球才可 ...
- 洛谷P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- [iOS Xcode注释插件]
来自onevcat的VVDocumenter-Xcode,地址是:https://github.com/onevcat/VVDocumenter-Xcode 使用方式:按三下"/" ...
- IDEA新建MAVEN项目时速度缓慢
原因 IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需要指定一个archetype-catalog.xml文件. 该命令的 ...
- 数据结构作业——max_and_min(栈)
Description TonyY 最近喜欢上了数学,今天他研究一个只有加号和乘号,运算数为整数, 大小在 1-9 之间的表达式,你可以任意地往里加括号,如何让表达式的值最大或 者最小? Input ...