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的更多相关文章

  1. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  2. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  3. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  4. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

  6. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  8. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

随机推荐

  1. win10 google浏览器设置

    在浏览器地址栏中输入命令: chrome://flags/ 撤销:chrome设置了禁止此页弹出提示框 chrome://settings/contentExceptions#popups

  2. 常用免费的WebService列表

    天气预报Web服务,数据来源于中国气象局 Endpoint :     http://www.webxml.com.cn/WebServices/WeatherWebService.asmx Disc ...

  3. Using the rJava package on Win7 64 bit with R

    加载 rJava 包报错: > library(rJava) Error : loadNamespace()里算'rJava'时.onLoad失败了,详细内容: 调用: fun(libname, ...

  4. 使用HttpSessionListener接口监听Session的创建和失效

    转自:http://uule.iteye.com/blog/824115 HttpSessionListener : Session创建事件发生在每次一个新的session创建的时候,类似地Sessi ...

  5. js-关于性能优化的一些学习总结

    性能优化的方法有: 1.减少HTTP请求:合并CSS/JS,使用CSS sprite等 2.压缩CSS/JS/图片 3.样式表放头部,JS放body底部:JS放在head中,将会等到js全部下载解析和 ...

  6. css-css权威指南学习笔记2

    第二章 选择器 1.在font属性中,只有一种情况下可以使用/来分隔两个特定的关键字,即元素的字体大小/行高. 2.通过把两个类选择器链接在一起,可以选择同时包含这些类名的元素,类名的顺序不限. .w ...

  7. 【caffe】create_cifar10.sh在windows下解决方案

    @tags caffe python windows下配置caffe后,create_cifar10.sh无法执行,因为是shell脚本.那就看懂脚本意思,用python重写一个: # create_ ...

  8. webapi获取IP的方式

    using System.Net.Http; public static class HttpRequestMessageExtensions { private const string HttpC ...

  9. RabbitMQ代码第一步

    Hello RabbitMQ 终于到了使用.Net连接RabbitMQ的时候了,我们首先新建一个控制台应用程序,在程序包管理控制器中NuGet中下载 RabbitMQ. Install-Package ...

  10. JSP+Servlet+javabean+mysql实现页面多条件模糊查询

    需求: 一般列表页上面会有一个查询框,有各种的查询条件组合,一般都采用模糊查询方式 ,以下以自己做的实例来说明一下实现方法: 需要实现的界面原型:要满足条件: 1.单选分类,点GO按扭 2.单独输入标 ...