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. Spring MVC(二)

    spring mvc工作流 1A)客户端发出http请求,只要请求形式符合web.xml 文件中配置的*.action的话,就由DispatcherServlet 来处理. 1B)Dispatcher ...

  2. 训练[2]-DFS

    题目A: 题目B[https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...

  3. java登录密码效验

    密码校验需求: 1) 密码控制只能输入字母.数字.特殊符号(~!@#$%^&*()_+[]{}|\;:'",./<>?)2) 长度 6-16 位,必须包括字母.数字.特殊 ...

  4. 为XYplorer添加右键菜单:“使用XYplorer打开”

    在目录.磁盘右键添加: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Shell\XYplorer]"E ...

  5. JavaScript动态加载资源【js|css】示例代码

    在开发过程中会用到各种第三方的插件,或者自己写在单独文件中的js方法库或者css样式,在html头部总是需要写一大堆的script和link标签,如果想要自己实现动态的引入资源文件,可以使用开源的re ...

  6. Nginx+Apache实现反向代理

    一 反向代理 1.1 反向代理是什么 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器, 并将从服务器上得到的结果返回给 ...

  7. 【第七篇】Volley之处理Gzip数据

    一般对于API请求需带上GZip压缩,因为API返回数据大都是Json串之类字符串,GZip压缩后内容大小大幅降低. public class GZipRequest extends StringRe ...

  8. Simple python reverse shell

    import base64,sys; import socket,struct s=socket.socket(2,socket.SOCK_STREAM) s.connect(('Attack's I ...

  9. CentOS/RedHat rpm方式安装Apache2.2

    注:所有RPM包均从网易镜像上下载 # rpm -ivh /home/apache/apr-1.3.9-5.el6_2.x86_64.rpm warning: /home/apache/apr-1.3 ...

  10. 点(.)运算符和箭头(->)运算符的区别

    本机中,char类型数据占用1byte, unsigned int, int, long int, float类型的数据占用4 bytes, double类型的数据占用8bytes. 至于指向所有基本 ...