每天一道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.
解决方法:
public class Solution {
public int longestPalindrome(String s) {
int size = s.length();
if (size == 0 || s == null)
return 0;
Map<Character,Integer> map = new HashMap<>();
for (int i = 0; i < size; i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i),map.get(s.charAt(i))+1);
} else {
map.put(s.charAt(i), 1);
}
}
int result=0;
boolean flag=false;
for(char c:map.keySet()){
int f=map.get(c);
if(f%2==0){
result+=f;
}else{
flag=true;
result+=f-1;
}
}
return result+(flag?1:0);
}
}
每天一道LeetCode--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 (最长回文)
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
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode——409. Longest Palindrome
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 【一天一道LeetCode】#9. Palindrome Number
一天一道LeetCode系列 (一)题目 Determine whether an integer is a palindrome. Do this without extra space. Some ...
- 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- flex中DataGrid里使用itemRenderer后数据无法绑定到数据源的问题
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="ht ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- ubuntu全盘备份与恢复
备份: 以下是我用来备份系统的完整命令: tar -jpcvf Ubuntu-12.04-20131018.tar.bz2 --exclude=/proc --exclude=/lost+found ...
- 【android开发】Android防止内存溢出浅析
近期项目做得差点儿相同了,測试出现了一些问题,当中一个就是内存溢出问题,在三星手机上測试最easy出现内存溢出,在其它手机上,比方华为就没有发生,也是比較郁闷.这个问题在之前的公司,做项目时也遇到过, ...
- UICollectionViewController
UICollectionViewController 目录 概述 UICollectionView UICollectionViewCell 代理方法 详细细节 概述 UICollectionView ...
- Tcsh脚本编程
Tcsh主要用于Free BSD等UNIX系统中. 一.输出字符串Hello的示例脚本 Tcsh脚本的基本格式.编写方法及脚本中使用的命令等,与Bash脚本完全相同,只需要直接套用即可. [root@ ...
- Django添加模型无法数据迁移解决方法
用Django开发一款博客,按照教程一步步写下来,发现当我创建一个模型blogpost的时候,使用数据迁移 python manage.py migrate 提示 Operations to perf ...
- 使用Thread类可以创建和控制线程
1.创建线程 static void Main(string[] args) { /* Thread类 * 创建控制线程 * 其构造函数接受ThreadStart和ParameterizedThrea ...
- JQuery实现回车代替Tab键(按回车跳到下一栏)
一个提交按钮以后,用户如果按了键盘的回车键,默认情况下,就会提交这个表单了.这样对于用户输入各个表单项目,用户体验很不好,输入完一个项目,或者用鼠标选择下一个项目,或者用键盘的Tab键选中下一个项目. ...
- B - 娜娜梦游仙境系列——跳远女王
B - 娜娜梦游仙境系列——跳远女王 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...