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. uinavigationcontroller uinavigationbar 下方横线去除

    #import "QKBaseNavigationController.h" #define fontSize 19 @interface QKBaseNavigationCont ...

  2. doT.js实例详解

    doT.js详细介绍 doT.js特点是快,小,无依赖其他插件.官网:http://olado.github.iodoT.js详细使用介绍 使用方法:{{= }} for interpolation{ ...

  3. 大型网站技术架构 核心原理与案例分析 pdf

    需要的小伙伴拿去,哈哈.百度云链接:https://pan.baidu.com/s/1hsuiM3q

  4. hibernate 和 jdbc 优缺点对比

    jdbc缺点: 1.编程繁琐,用的try和catch比较多 2.jdbc没有对数据做缓存 3.没有做到面向对象编程 4.sql语句的跨平台性很差 jdbc优点: 效率比较高 hibernate的优点: ...

  5. Python基础篇-day7

    本节目录-面向对象1 类介绍1.1 面向对象oo特征1.2 类的特性1.3 创建与调用 1.3.1 基本结构 1.3.2 结构说明 1.3.3 对外部提供只读访问接口 1.3.4 析构方法2 继承2. ...

  6. 难以记住的sql语句

    天,把这篇文章转移到这里,增强一下记忆,找起来也更方便. 导出: mysqldump -u username -p password -h hname dbname tblname > file ...

  7. 《JS权威指南学习总结--第7章 数组》

    内容要点: 一. JS数组是无类型的:数组元素可以是任意对象,并且同一个数组中的不同元素也可能有不同的类型.数组的元素甚至也可能是对象或其他属性,这允许创建复制的数据结构,如对象的数组和数组的数组. ...

  8. HDU 1596 find the safest road(SPFA)

    Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...

  9. php基础(五)日期

    PHP Date() 函数 date(format,timestamp) 参数 描述 format 必需.规定时间戳的格式. timestamp 可选.规定时间戳.默认是当前时间和日期. 注释:时间戳 ...

  10. delphi 中sql的语法规范

    1.引号配对:  这是在Delphi使用SQL语句时容易出错的地方,由于delphi规定在字符串中用两个西文的单引号“''”表示一个“'”,在拼装语句的时候就容易疏忽遗漏.  Delphi里有个函数Q ...