当前位置:网站首页>【LeetCode】Day109-the longest palindrome string
【LeetCode】Day109-the longest palindrome string
2022-08-01 18:21:00 【The other way around is loops】
题目
题解
思路
The palindrome must be symmetric,Since it is mentioned symmetry,This means that each letter needs to appear an even number of times,At most one letter appears an odd number of times,且为1次(贪心思路)
算法
set letterch出现v次,那么我们可以使用ch共 v / 2 ∗ 2 v/2*2 v/2∗2次,There is an odd number of occurrences of any letter,Then it can be used as the center of the palindrome,Center at most only1个.
class Solution {
public int longestPalindrome(String s) {
int n=s.length();
//统计字符出现次数
int[] count=new int[128];
for(int i=0;i<n;i++){
char c=s.charAt(i);
count[c]++;
}
//最长回文串长度
int res=0;
for(int i='A';i<='z';i++){
res+=count[i]/2*2;
//Odd times and no palindrome center
if(count[i]%2==1&&res%2==0)
res++;
}
return res;
}
}
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( S ) O(S) O(S),其中 S 为字符集大小
边栏推荐
- Leetcode74. 搜索二维矩阵
- 顺序表的简单描述及代码的简单实现
- Basic image processing in opencv
- 金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--章节实验
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
- Leetcode75. 颜色分类
- Topology零部件拆解3D可视化解决方案
- 【Day_08 0426】两种排序方法
- Topology Parts Disassembly 3D Visualization Solution
- 塔防海岸线用户协议
猜你喜欢

QT commonly used global macro definitions

MySQL 45 Talk | 09 How to choose common index and unique index?

XAML WPF项目groupBox控件

亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技

opencv如何实现图像倾斜校正

Solve the problem that MySQL cannot insert Chinese data

Summer vacation second week wrap-up blog
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近

B001 - 基于STM32的智能生态鱼缸

B001 - Intelligent ecological fish tank based on STM32
随机推荐
Go GORM事务实例分析
odoo+物联网
关于单应性矩阵的若干思考
OpenCV installation, QT, VS configuration project settings
Summer vacation second week wrap-up blog
How opencv implements image skew correction
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
tooltip 控件
1065 A+B and C (64bit)
【Day_12 0507】二进制插入
MySQL数据库————存储过程和函数
Leetcode75. 颜色分类
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
opencv基本的图像处理
QT基础功能,信号、槽
Live chat system technology (8) : vivo live IM message module architecture practice in the system
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
Zabbix6.0钉钉机器人告警
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution