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.

Input:
"abccccdd" Output:
7 Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
 
寻找字符串重组之后能存在的最长的回文串。
思路清晰:
用数组记录出现相同字符的个数,只要个数大于2个那么一定能在最后的回文串中出现,如果有单个的只能最多增加以一个长度。
public class Solution {
public int longestPalindrome(String s) {
int[] str = new int[123]; for (int i=0;i<s.length();i++){
str[s.charAt(i)]++;
} int flag = 0;
int sum = 0; for (int i=65;i<=90;i++){
if(str[i] > 1){
sum += str[i];
if(str[i]%2 != 0){
flag = 1;
sum--;
}
}else if (str[i] == 1)
flag = 1;
} for (int i=97;i<=122;i++){
if(str[i] > 1){
sum += str[i];
if(str[i]%2 != 0){
flag = 1;
sum--;
}
}else if (str[i] == 1)
flag = 1;
} return sum + flag;
}
}
然后根据高手提示,还可以少很多代码,如果最后的回文串长度小于原来的长度,直接加一就可以了,如果和原来相同则不加,因为多余下来的只能有一个字符处于最后回文串的中间位置。
更改后代码是这样的。
public class Solution {
public int longestPalindrome(String s) {
int[] str = new int[123]; for (int i=0;i<s.length();i++)
str[s.charAt(i)]++; int sum = 0; for (int i=65;i<=122;i++)
sum += str[i]/2; sum *= 2; if(sum < s.length())
return sum+1;
else
return sum;
}
}

leetcode409的更多相关文章

  1. 每天一道LeetCode--409 .Longest Palindrome

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

  2. [Swift]LeetCode409. 最长回文串 | Longest Palindrome

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

  3. LeetCode 349,350 数组的交集

    LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...

随机推荐

  1. 【Python之路】第十一篇--CSS

    css CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css的四种引入方式 1.行内式 行内式是在标记的 ...

  2. redis5--set的操作

    Set集合类型(1)介绍redis的set是string类型的无序集合set元素最大可以包含(2的32次方-1)个元素关于set集合类型除了基本的添加删除操作,其它有用的操作还包含集合的取并集(uni ...

  3. Selenium 中文API

    Selenium 中文API   转自:http://blog.csdn.net/lh9529/article/details/3946567 概念 Selenium 通过命令进行驱动.Seleniu ...

  4. 3.linux常用软件的安装方法

    linux 上的软件不像windows上直接运行安装那么容易,在linux上有很多不同的安装包,大概常见的就有deb.tar.gz.tar.bz(tar.bz2).rpm等类型文件 1.deb文件安装 ...

  5. 第一章 flume架构介绍

    1.flume概念介绍 1.1 常见的分布式日志收集系统                             Scribe是facebook开源的日志收集系统,在facebook内部已经得到大量的 ...

  6. jQuery切换网页皮肤并保存到Cookie示例代码

    经过使用,靠谱! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  7. Java Swing 日期控件(转载)

    http://www.cnblogs.com/lzy1991/p/5714935.html

  8. input、button、a标签 等定义的按钮尺寸的兼容性问题

    在项目中有遇到这类问题,搜索了一下解决方式,采用链接:https://segmentfault.com/q/1010000000190931 里各位楼主的答案,摘抄如下: 例子如下: HTML: &l ...

  9. article标签和aside标签两者的理解

    article标签,使用后感觉和P(段落)差不多,语义化的标签.<aside> 标签定义article以外的内容(可用做文章的侧栏). 语义化的标签.  html 标签有几种分类,其中有一 ...

  10. laravel5.2/laravel5.3入门指南 Windows 上快速安装并运行 Laravel 5.x

    1 首先要搭建本地服务器环境推荐phpstudy2016及wampServer3.0.6 下载链接可参考 http://www.cnblogs.com/zzcit/p/5823742.html 注意一 ...