原题链接在这里:https://leetcode.com/problems/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.

题解:

Time Complexity: O(s.length()). Space: O(s.length()).

AC Java:

 public class Solution {
public int longestPalindrome(String s) {
if(s == null || s.length() == 0){
return 0;
} int res = 0;
HashSet<Character> hs = new HashSet<Character>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(hs.contains(c)){
hs.remove(c);
res++;
}else{
hs.add(c);
}
}
return hs.isEmpty() ? res*2 : res*2+1;
}
}

类似Palindrome Permutation.

LeetCode Longest Palindrome的更多相关文章

  1. [LeetCode] Longest Palindrome 最长回文串

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

  2. [LeetCode] Longest Palindrome Substring 具体分析

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

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

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

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

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

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

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

  6. LeetCode 409 Longest Palindrome

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

  7. 24. leetcode 409. Longest Palindrome

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

  8. 【leetcode】409. Longest Palindrome

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

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

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

随机推荐

  1. ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机

    ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机 使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linu ...

  2. java不用jni,也可以获得当前系统性能信息

    最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o... 在Java中,可以获得总的 ...

  3. 关于实现banner轮换的问题,如何修改

    最近遇到了这样的问题,本来banner都是gif格式的,但是现在要求上传图片格式为jpg时,运用JS实现动画效果,原来的也能用. aspx: <div id="bh" run ...

  4. [简单]poi word2007表格按模版样式填充行数据

    主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数 ...

  5. Qt5.4 VS2010 Additional Dependancies

    Go to Linker -> General -> Additional LIbrary Directories: qtmaind.libQt5Cored.libQt5Guid.libQ ...

  6. C#常用功能函数小结(.NET 4.5)

    今天有空,把C#常用的功能总结一下,希望对您有用.(适用于.NET Framework 4.5) 1. 把类转换为字符串(序列化为XML字符串,支持xml的namespace) using Syste ...

  7. MemPool

    腾讯笔试题,设计内存池,alloc和free都是O(1). 和LRUCache类似,这里用了一个list表示可用的空间,用一个map来记录这块内存是否已分配,这样free的时候才可能O(1). cla ...

  8. 在Windows2003上安装Active Directory Management Gateway Service

    为了让基于Windows2003的域控能够被Powershell管理,必须安装KB968934,但是直接安装会报以下的错误,必须先安装NDP35SP1-KB969166.但是"因为基于 Wi ...

  9. Java集合之Collection接口

    java的集合分为三大接口,分别是Collection,Map,Iterator,集合接口和类在java.util包中,此次主要介绍三大接口之一的Collection接口. 一些Collection允 ...

  10. 红米note3的wifi断流或假死

    红米note3的wifi断流/假死 日常使用note3的时,比如长时间浏览网页,点击一个链接会卡住不动,在等待十几秒之后才恢复.第一反应是不是网络不好?但是这种情况常常出现之后,对比其他的手机,比如价 ...