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 方法三 ...
 
随机推荐
- JavaScript的DOM操作-重点部分-第一部分
			
Window.document 对象 一.找到元素 document.getElementById("id"); 根据id找,最多找一个: var a = document.get ...
 - SpringMVC与Struts2区别与比较总结
			
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...
 - yii2的扩展程序包
			
查找yii2的扩展程序包 https://packagist.org/ 搜索yiisoft/yii2 可通过composer install下载 composer install下载程序包是通过com ...
 - bzoj 3223 splay模板题3
			
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
 - css-使用line-height实现垂直居中的一些问题
			
网上都是这么说的,把line-height值设置为height一样大小的值可以实现单行文字的垂直居中.这句话确实是正确的,但其实也是有问题的.问题在于height,看我的表述:"把line- ...
 - 【BZOJ-4547】小奇的集合        矩阵乘法 + 递推
			
4547: Hdu5171 小奇的集合 Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 175 Solved: 85[Submit][Status][D ...
 - Maven配置不成功
			
配置了MAVEN_HOME,新建了java文件,在d:/java/MAVEN_HOME/apach....,path下输出%MAVEN_HOME%bin,为什么cmd下mvn不行呢?因为MAVEN_H ...
 - 【bzoj1034】 ZJOI2008—泡泡堂BNB
			
http://www.lydsy.com/JudgeOnline/problem.php?id=1034 (题目链接) 题意 田忌赛马.. Solution 贪心. 1.若A队最弱的比B队最弱的强,先 ...
 - Newton-Raphson算法简介及其R实现
			
本文简要介绍了Newton-Raphson方法及其R语言实现并给出几道练习题供参考使用. 下载PDF格式文档(Academia.edu) Newton-Raphson Method Let $f(x) ...
 - 深入分析ConcurrentHashMap
			
术语定义 哈希算法 hash algorithm 是一种将任意内容的输入转换成相同长度输出的加密方式,其输出被称为哈希值. 哈希表 hash table 根据设定的哈希函数H(key)和处理冲突方 ...