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 ...
随机推荐
- MYSQL 重新设置自增值
说明:当一张有auto_increment主键的表中存有10条数据,现删除5-10条数据, 再insert数据的时候auto_increment将从11开始. 现在我们改变它设为从6开始. ALTER ...
- Windows下安装pillow、opencv库问题,亲测可行
安装pillow 库 python -m pip install pillow 出自:http://blog.csdn.net/Riverhope/article/details/78766969 安 ...
- [Luogu4169][Violet]天使玩偶/SJY摆棋子
luogu 题意 一个平面上有\(n\)个点,\(m\)次操作,每次新增一个点,或者是询问离某个点最近的点的距离.这里的距离是曼哈顿距离. \(n,m\le3*10^5\) sol 写一发\(CDQ\ ...
- 转载 关于case语句的优先级
对于这样的组合逻辑电路 always@(X) case(X) X1: X2: …… endcase 如果分支项包含变量X的所有取值情况,并且互相不重复,那么这样的情况,其实没有必要使用综合指令. (一 ...
- 【策略】一致性Hash算法
转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...
- sed 相关
sed中,a命令是指在匹配的位置后面插入新的内容. c命令是说将在匹配模式空间的指定行用新文本加以替代.
- php foreach 跳出本次循环/当前循环与终止循环的方法
PHP中用foreach()循环中,想要在循环的时候,当满足某个条件时,想要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continue 与 brea ...
- 蓝桥杯 算法训练 ALGO-60 矩阵乘法
算法训练 矩阵乘方 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个矩阵A,一个非负整数b和一个正整数m,求A的b次方除m的余数. 其中一个nxn的矩阵除m的余数得到的仍是一个 ...
- easyui的datagird动态设置当前页数
if (ishas) { $("#tg").datagrid("options").pageNumber = 1; $('#tg').datagrid('rel ...
- videojs集成--播放rtmp流
之前说到已经把流推送过来了,这时候就可以使用videojs来进行显示播放. 首先要先有一个文件,那就是video-js.swf 因为,这种播放方式html已经不能很好的进行播放了,需要用到flash来 ...