

#yyds干货盘点# leetcode算法题:最长回文串
source link: https://blog.51cto.com/u_13321676/5539130
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# leetcode算法题:最长回文串
原创给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 。
在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。
输入:s = "abccccdd"
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
输入:s = "a"
输入:s = "bb"
代码实现:
public int longestPalindrome(String s) {
int[] count = new int[128];
int length = s.length();
for (int i = 0; i < length; ++i) {
char c = s.charAt(i);
count[c]++;
}
int ans = 0;
for (int v: count) {
ans += v / 2 * 2;
if (v % 2 == 1 && ans % 2 == 0) {
ans++;
}
}
return ans;
}
}
Recommend
-
33
题目: 给定一个字符串 s ,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 思路: 看到这道题目,首先想到回文字符串,是一个沿正中字...
-
11
LeetCode学习笔记——最长回文子串
-
5
#yyds干货盘点# leetcode算法题:回文数 原创 灰太狼_cxh 2022-05-26 09:...
-
8
#yyds干货盘点# leetcode算法题:有效的括号 原创 灰太狼_cxh 2022-06-06...
-
6
#yyds干货盘点# leetcode算法题:两数相除 原创 灰太狼_cxh 2022-06-13 0...
-
4
#yyds干货盘点# leetcode算法题:最长公共前缀 原创 灰太狼_cxh 2022-08-...
-
5
#yyds干货盘点# leetcode算法题:回文链表 原创 灰太狼_cxh 2022-08-15 1...
-
3
#yyds干货盘点# 动态规划专题:最长回文子序列 精选 原创 97的风 2022-11-01 17:5...
-
10
#yyds干货盘点# 名企真题专题: 回文串 精选 原创 97的风 2022-12-31 13:04:02
-
5
#yyds干货盘点# LeetCode面试题:回文数 精选 原创 97的风 2023-02-09 18:04:56
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK