[抄题]:

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. 多想一些情况:最后剩余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 最长对称串的更多相关文章

  1. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. 409 Longest Palindrome 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...

  3. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  4. LeetCode 5 最长对称串

    LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...

  5. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  6. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  9. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

随机推荐

  1. Oracle:Create, alter, drop and add!

    Oacle 下对标下元素约束条件的添加与删除 插入元素 alter table table_name add add_name data_type; 添加约束条件 NOT NULL alter tab ...

  2. Jolt Awards: The Best Books

    Jolt大奖素有“软件业界的奥斯卡”之美誉,共设通用类图书.技术类图书.语言和开发环境.框架库和组件.开发者网站等十余个分类,每个分类设有一个“震撼奖”(Jolt Award)和三个“生产力奖”(Pr ...

  3. BZOJ - 4771 七彩树 (可持久化线段树合并)

    题目链接 对每个结点建立两棵线段树,一棵记录该结点的子树下每种颜色对应的最小深度,另一棵记录子树下的每个深度有多少结点(每种颜色的结点只保留最浅的深度即可),自底而上令父节点继承子结点的线段树,如果合 ...

  4. 《DSP using MATLAB》示例Example7.21

    代码: h = [1, 2, 3, 4, 3, 2, 1]/15; M = length(h); n = 0:M-1; [Hr, w, a, L] = Hr_Type1(h); a L amax = ...

  5. 剑指offer-第六章面试中的各项能力(数字在排序数组中出现的次数)

    题目:统计一个数字在排序数组中出现的次数. 思路:采用二分查找,找到该数字在数组中第一次出现的位置,然后再找到组后一个出现的位置.两者做减法运算再加1.时间复杂度为O(logn) Java代码: // ...

  6. hadoop复合键排序使用方法

    在hadoop中处理复杂业务时,需要用到复合键,复合不同于单纯的继承Writable接口,而是继承了 WritableComparable<T>接口,而实际上,WritableCompar ...

  7. Cocos2d-x -自己定义动作 圆周运动

    原文地址:http://blog.csdn.net/u012945598/article/details/17605409 在之前的文章中我们以前讲过Cocos2d-x中的各种动作的用法,我们先来简单 ...

  8. The type org.springframework.context.ConfigurableApplicationContext cannot be resolved问题解决

    在搭建maven项目的时候,有时候会报这样的问题. The type org.springframework.context.ConfigurableApplicationContext cannot ...

  9. mysql数据库备份脚本

    mysql数据库备份脚本 mysql数据库分库备份脚本:[root@localhost tmp]# cat mysql.sh #!/bin/bash USER=root PASSWORD=joy4yo ...

  10. MyBatis collection的两种形式——MyBatis学习笔记之九

    与association一样,collection元素也有两种形式,现介绍如下: 一.嵌套的resultMap 实际上以前的示例使用的就是这种方法,今天介绍它的另一种写法.还是以教师映射为例,修改映射 ...