leetcode409
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的更多相关文章
- 每天一道LeetCode--409 .Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 349,350 数组的交集
LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...
随机推荐
- 301跳转:IIS服务器网站整站301永久重定向设置方法(阿里云)
欢迎来到重庆SEO俱乐部:搜索引擎优化学习交流QQ群224306761. 承接:seo优化.网站建设.论坛搭建.博客制作.全网营销 博主可接:百度百家.今日头条.一点资讯等软文发布,有需要请联系PE! ...
- TortoiseGit HTTPS方式保存密码最简单的方法
在TortoiseGit的设置 -> git 中选择 编辑本地 .git/config 在最后增加下面内容: [credential] helper = store
- git中添加多个SSH公钥,以及不同系统之间的差别
git学习已经也有一段时间了,基本的操作,口令会了一点点,但是还没有git团队开发的实践,这个有待加强~ git是在windows下面模拟Linux环境的,linux自带的ssh服务,可以通过该服务远 ...
- 奇葩json结构解析--key是数字的json处理
json结构如下: { "ret": "ok", "data": { "57230": { "cat_id&q ...
- How to solve java.net.SocketTimeoutException:60000millis problem in HDFS
Many HDFS users encounter the following error when DFSClient ready file from a certain Data Node. & ...
- toString--->转字符串
因为它是Object里面已经有了的方法,而所有类都是继承Object,所以“所有对象都有这个方法”.它通常只是为了方便输出,比如System.out.println(xx),括号里面的“xx”如果不是 ...
- 2016年团体程序设计天梯赛-决赛 L1-7. 到底是不是太胖了(10)
据说一个人的标准体重应该是其身高(单位:厘米)减去100.再乘以0.9所得到的公斤数.真实体重与标准体重误差在10%以内都是完美身材(即 |真实体重-标准体重| < 标准体重x10%).已知市斤 ...
- 利用ckeditor 富文本编辑插件静态化网页
// step5: 生成静态化html FileOutputStream fos = null; PrintStream printStream = null; ...
- Codeforces Round #366 (Div. 2)_B. Spider Man
B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 点击其他地方隐藏div
document.onclick = function(e){ var ele = e?e.target:window.event.srcElement; if(ele.id !== 'valueSh ...