【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/longest-palindrome/
- Difficulty: Easy
题目描述
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.
题目大意
判断一个字符串能够成的最长的回文字符串长度是多少。
解题方法
方法一:字典统计次数
题目的意思是找出字符串能够成的最长的回文字符串的长度。
思路是需要找出规律:
- 先加上所有能够成偶数次的字符串次数:
- 如果一个字符出现了偶数次,那么一定可以是最终回文字符串的一部分,加上该字符出现的次数;
- 如果一个字符出现了奇数v次,那么可以加上v-1次(只使用偶数次这个字符);
- 如果有出现过奇数次的字符,那么最后结果+1,代表把该奇数字符放在中间。
Python解法:
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
count = collections.Counter(s)
res = 0
prime = 0
for k, v in count.items():
if v % 2 == 1:
res += v - 1
prime = 1
else:
res += v
return res + prime
C++代码如下:
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char, int> count;
for (char c : s)
++count[c];
int res = 0;
bool hasOne = false;
for (auto d : count) {
if (d.second % 2 == 0)
res += d.second;
else {
res += d.second - 1;
hasOne = true;
}
}
if (hasOne)
++res;
return res;
}
};
方法二:HashSet
看了高票的答案,可以HashSet的方法,统计一个字符是否出现了偶数次,一增一减相抵消,这样的个数*2,如果HashSet中还有元素再加上1即可。
Java代码如下:
public class Solution {
public int longestPalindrome(String s) {
HashSet<Character> hashset=new HashSet<Character>();
int count=0;
for(int i =0; i< s.length(); i++){
if(hashset.contains(s.charAt(i))){
hashset.remove(s.charAt(i));
count++;
}else{
hashset.add(s.charAt(i));
}
}
if(!hashset.isEmpty()) return count*2 +1;
return count*2;
}
}
AC: 23 ms
虽然只遍历了一遍,但是用到了HashSet,因此效率没我的高。
方法三:次数除以2再乘以2
用两个int[26],分别保存大小写字符,统计字符出现的对数,最后判断这些字符对数相加是否等于原字符的长度,如果不等说明有奇数的出现,在/2的时候被舍去了。方法思想挺巧妙的。
Java代码如下:
public int longestPalindrome(String s) {
int[] lowercase = new int[26];
int[] uppercase = new int[26];
int res = 0;
for (int i = 0; i < s.length(); i++){
char temp = s.charAt(i);
if (temp >= 97) lowercase[temp-'a']++;
else uppercase[temp-'A']++;
}
for (int i = 0; i < 26; i++){
res+=(lowercase[i]/2)*2;
res+=(uppercase[i]/2)*2;
}
return res == s.length() ? res : res+1;
}
日期
2017 年 1 月 8 日
2018 年 11 月 16 日 —— 又到周五了!
2019 年 2 月 18 日 —— 继续学习~
2020 年 3 月 19 日 —— 恰巧每年都做一遍这个题
【LeetCode】409. Longest Palindrome 解题报告(Python & C++)的更多相关文章
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- [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 (最长回文)
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 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- 富集分析DAVID、Metascape、Enrichr、ClueGO
前言 一般我们挑出一堆感兴趣的基因想临时看看它们的功能,需要做个富集分析.虽然公司买了最新版的数据库,如KEGG,但在集群跑下来嫌麻烦.这时网页在线或者本地化工具派上用场了. DAVID DAVID地 ...
- Google服务器架构图解简析
无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...
- 【玩具】获取B站视频的音频片段
事情是这样的,我有个和社畜的社会地位不太相符的小爱好--听音乐剧. 基本上是在B站上点开视频听,不是不想在网易云或者QQ音乐听,只是在这些音乐软件上面,我想听的片段要不就收费,要不版本不是我喜欢的,要 ...
- 巩固java第六天
巩固内容: HTML 空元素 没有内容的 HTML 元素被称为空元素.空元素是在开始标签中关闭的. <br> 就是没有关闭标签的空元素(<br> 标签定义换行). 在 XHTM ...
- 3步!完成WordPress博客迁移与重新部署
本文来自于轻量应用服务器征文活动的用户投稿,已获得作者(昵称nstar)授权发布. 由于现有的服务器已经到期,并且活动已经取消,续费一个月145元比较贵,于是参加了阿里云的活动购买一台轻量应用服务器. ...
- Erda 系列 Meetup「成都站」携手SOFAStack 和你聊聊云原生基础设施建设那点事儿
技术控快上车啦秋天的第一场活动一起来收获技术干货吧! 主题: 云原生基础设施建设的现在及未来时间: 2021 年 9 月 11 日 (周六) 13:30-17:00活动地点: 四川省成都市蚂蚁 C 空 ...
- A Child's History of England.23
King William, fearing he might lose his conquest, came back, and tried to pacify the London people b ...
- 事务(@Transactional注解)的用法和实例
参数 @Transactional可以配制那些参数及以其所代表的意义: 参数 意义 isolation 事务隔离级别 propagation 事务传播机制 readOnly 事务读写性 noRollb ...
- ORACLE lag,lead
oracle中想取对应列前几行或者后几行的数据时可以使用lag和lead分析函数 lag:是滞后的意思,表示本行数据是要查询的数据后面,即查询之前行的记录. lead:是领队的意思,表示本行数据是要查 ...
- weak和拷贝
weak/拷贝 1. weak 只要没有strong指针指向对象,该对象就会被销毁 2. 拷贝 NSString和block用copy copy语法的作用 产生一个副本 修改了副本(源对象)并不会影响 ...