LeetCode Longest Palindrome
原题链接在这里: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;
}
}
LeetCode Longest Palindrome的更多相关文章
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Longest Palindrome Substring 具体分析
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- 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&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- 李洪强-C语言6-控制结构
C语言流程控制 一.流程控制结构 (1)顺序结构:按书写顺序执行每一条语句. (2)选择结构:对给定的条件进行判断,根据判断结果决定执行哪一段代码. (3)循环结构:在给定条件成立的情况下,反复执行某 ...
- DTD约束的校验工具安装及检验(Iexmltls工具)
通过打开我们写的dtd约束文档,我们可以看到,在我们不按规定的格式打开xml时并不能检验出错误.此时我们可以借助软件来帮助我们校验. Iexmltls是一个在IE浏览器下安装的用于检验xml约束是否正 ...
- c#中分布方法和分部类
将同一个类编写在多个文件中,类的各个文件名不同,类名相同,类名前加partial关键字,这种类型叫分部类. 在分部类中可以建立分部方法,方法名前加关键字partial,分部方法只能将方法分成两部分,即 ...
- Css - 文本溢出处理
http://blog.163.com/yinwei_666/blog/static/2036157320101113102733794/ overflow: hidden;-o-text-overf ...
- SiteMesh装饰器使用总结
SiteMesh是一个Java WEB项目的网页布局和修饰框架.使用SiteMesh后就不再需要在每个页面中都用<jsp:include>标签引入页头.页尾.导航等其他公用页面了. 可以将 ...
- jQuery 中 on 方法-----给未来元素添加事件
<li class='clear dir-li'> <div class='left title'> 添加到目录:</div> <div class='lef ...
- static的作用,this(),super()用法
1:static{}表示静态代码块:在java虚拟机(jvm)加载该类时,会执行这个代码块一次,静态代码块在new()对象之前就加载了 2: this()与surper()区别:surper()是从子 ...
- JS初学者必备的几个经典案例(二)!!!
一.写出当前年份的前后5年的日期表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 在Delphi中如何动态创建dbf数据库(二)?
unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...
- oracle创建dblink问题
1.如果在本地"D:\oracle\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora"中设置 服务器数据库连接,必须再服务器设置 ...