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. How to use php serialize() and unserialize()

    A PHP array or object or other complex data structure cannot be transported or stored or otherwise u ...

  2. Linux常用命令汇总及使用方法(二)之文本编辑器VI

    VI可能是在Linux中使用比较频繁的文本编辑器,如果不能熟练使用VI,在一定程度上会影响工作效率,所以在这里记录一下VI的常用命令及操作方式 在[root@test ~]# vi carrie.tx ...

  3. sf中标准的分页功能介绍

    世上本无事,庸人自扰之.我喜欢一个相对比较安静的环境去学习和工作,希望在一个掉一根针的声音都能够听到的环境中,但是有时候往往相反,一片嘈杂,我改变不了周围的环境,只能改变自己,其实这些都没有什么,也许 ...

  4. slf4j 之logback日志之环境安装【一】

    一.maven引用. 传送门:http://www.slf4j.org/manual.html#projectDep <dependency> <groupId>ch.qos. ...

  5. Adding DOM elements to document

    1.JavaScript 添加DOM Element 执行效率比较: 抄自:http://wildbit.com/blog/2006/11/21/javascript-optimization-add ...

  6. ubuntu 14.04 opencv2.4.13 安装

    1.下载然后解压安装压缩包 unzip opencv-2.4.13.zip 2. 进入刚解压的文件夹,建立release文件夹 cd opencv-2.4.13 mkdir release 3. 安装 ...

  7. python+selenium+Eclipse安装

    1.安装python 参考安装python:http://www.cnblogs.com/beyongblue/p/4215740.html 2.安装python管理工具setuptools 3.安装 ...

  8. javaWEB总结(11):JSP简介及原理

    前言 本文主要通过一个简单小例子,介绍JSP的原理. 1.项目结构 2.web.xml <?xml version="1.0" encoding="UTF-8&qu ...

  9. android 5.0 -- Activity 过渡动画

    android 5.0 提供3种过渡动画: 进入 退出 进入退出包括如下效果: explode 分解:屏幕中间进出 slide 滑动:屏幕边缘进出 fade 淡出:改变透明度来添加或者移除视图 共享 ...

  10. 淘淘商城_day10_课堂笔记

    今日大纲 Dubbo入门学习 使用dubbo优化单点登录系统 系统间服务调用方式 浏览器直接访问 浏览器发起请求,通过ajax或jsonp方式请求: Httpclient方式 系统与系统之间通过Htt ...