409. Longest Palindrome 最长对称串
[抄题]:
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.
Note:
Assume the length of given string will not exceed 1,010.
Example:
Input:
"abccccdd" Output:
7 Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道多余了一些单独的字母时,应该怎么处理
[一句话思路]:
先处理成对的,单独的在结果处操作
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 多想一些情况:最后剩余1个字母、多个字母,都是count * 2 + 1
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
先处理成对的,单独的在结果处操作
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
set-remove
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int longestPalindrome(String s) {
//cc
if (s.length() == 0) {
return 0;
}
//ini:set
Set set = new HashSet();
int count = 0;
//for loop: contains, count+1
for (char c : s.toCharArray()) {
if (! set.contains(c)) {
set.add(c);
}else {
set.remove(c);
count++;
}
}
//res count * 2 + 1
if (set.size() != 0) {
return count * 2 + 1;
}else {
return count * 2;
}
}
}
409. Longest Palindrome 最长对称串的更多相关文章
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- LeetCode 5 最长对称串
LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 409. Longest Palindrome (最长回文)
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode——409. Longest Palindrome
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...
随机推荐
- I.MX6 Android 设备节点权限
/********************************************************************************** * I.MX6 Android ...
- linux中的vim编辑器的使用
vim的三种模式: 常规模式(命令模式)也是默认模式,从其它模式进行命令模式按esc i 在光标前插入文本 o 命令是指在当前行下方插入新行 dd 是删除光标所在的整个一行 yy 是在光标所在整个放入 ...
- 使用iptables nat进行端口转发
1.将发向HostA:PortA的请求转发到HostB:PortB iptables -t nat -A PREROUTING -p tcp -i eth0 -d HostA --dport Port ...
- 【模板】NOIP模板汇总
图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...
- 定时任务&&找出两个list的不同
/*-------------------------application-context.xml------------------------------*/ <?xml version= ...
- Server Error in '/' Application. IIS拒绝访问
昨天将改好的网站重写发布更新了一下,就出现这种问题.那是一个头两个大呀. 刚开始以为是文件夹没有IIS的访问权限,在网上找的好多答案都是temp文件夹没有权限,,但将IIS的权限都加上后,还是不行,同 ...
- HTML5视频直播及H5直播扫盲
章来源:http://geek.csdn.net/news/detail/95188 分享内容简介: 目前视频直播,尤其是移动端的视频直播已经火到不行了,基本上各大互联网公司都有了自己的直播产品,所以 ...
- 【转】 Pro Android学习笔记(九三):AsyncTask(2):小例子
目录(?)[-] 继承AsyncTask UI操作接口 使用AsyncTask 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn. ...
- 【转】JMeter中对于Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- java代码switch语句求分数等级
总结:从键盘输入分数----- 如果在0到100内,则输出等级 小于0或者是大于100都不能输出,这里用if-else条件判断. package com.c2; import java.util.Sc ...