LeetCode 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.
题目标签:Hash Table
题目给了我们一个string s,让我们找到在这个s 里 最长可能性的回文的长度。
利用HashMap 先把 s 里的所有char 和它出现次数 做记录,然后遍历map 的keySet,把所有char 长度是偶数的加起来,在把所有char 长度是奇数的 - 1 加起来,最后要判断一下,如果有出现过奇数长度的char,那么在最后答案上再 + 1。
Java Solution:
Runtime beats 58.91%
完成日期:06/06/2017
关键词:HashMap
关键点:对于奇数长度的char,我们也需要把它的 长度 - 1 累加
class Solution
{
public int longestPalindrome(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
int len = 0;
boolean oddChar = false; for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); for(char c: map.keySet())
{
int temp_len = map.get(c); if(temp_len % 2 == 0) // even len
len += temp_len;
else // odd len
{
if(!oddChar)
oddChar = true; len += temp_len - 1;
}
} return oddChar ? len + 1 : len;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 409. Longest Palindrome (最长回文)的更多相关文章
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Longest Palindrome 最长回文串问题
1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 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: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode之“字符串”:最长回文子串
题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...
- LeetCode——409. Longest Palindrome
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
- 转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...
随机推荐
- Hive扩展功能(二)--HWI接口
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- 字符流-缓冲区-自定义myBufferedReader
public class myBufferedReaderDemo { public static void main(String[] arg) throws IOException{ FileRe ...
- ANNOTATION and analyse hello1.java
一.What is annotation? annotation的中文意思就是注解,注释的意思.注解也属于一种类型.它是在 Java SE 5.0 版本中开始引入的概念.它的形式跟接口很类似,不过前面 ...
- 链表相关的leetcode重要题目
Leetcode 92:反转链表II 解决这道题需要三个步骤: 找到需要反转的第一个节点.可以通过头节点前进m-1步,找到反转开始的位置. 将需要反转的部分进行反转.参考Leetcode 206:反转 ...
- 华登区块狗系统APP开发
华登区块狗系统开发,陈翎:{.l8O..285l..l22O.}华登区块狗软件开发,华登区块狗APP开发,华登区块狗模式开发,华登区块狗现成源码,狗狗集市理财模式开发 华登区块狗是什么?华登区块狗ap ...
- JPQL 的基本使用
一.概念 JPQL 语言,即 Java Persistence Query Language 的简称.JPQL 和 HQL 是非常类似的,支持以面向对象的方式来写 SQL 语句,当然也支持本地的 SQ ...
- (C/C++学习)20.基于C++改进的单目标遗传算法
说明:在学习生活中,经常会遇到各种各样的最优问题,其中最常见的就是求某个多维(多个自变量)函数在各个自变量各取何值时的最大值或最小值:例如求函数 f(x) = (x-5)2+(y-6)2+(z-7)2 ...
- UVA-1599 Ideal Path(双向BFS)
题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...
- ubuntu解压zip文件出现乱码情况解决方法
使用 unzip datastructure.zip 出现下面的情况: extracting: └╧╗╞/╗·╞ў╤з╧░╝п╜ї/╩¤╛▌╜с╣╣╙ы╦у╖и/╩¤╛▌╜с╣╣╙ы╦у╖иги2гй ...
- <Zookeeper>入门 概念
分布式协调服务 Zookeeper 分布式环境的特点 1.分布性 基于一个硬件或者多个硬件设备以及多个软件组成的分布在不同网络计算机上的系统架构,通过消息传递进行通信协调.在空间上部署是可以任意的,网 ...