当前位置:网站首页>【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 为字符集大小
边栏推荐
- 一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
- SQL的索引详细介绍
- B002 - Embedded Elderly Positioning Tracking Monitor
- 云原生全景图详解
- Prometheus的Recording rules实践
- AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
- 电商库存系统的防超卖和高并发扣减方案
- XML配置
- Basic image processing in opencv
- OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
猜你喜欢
QT_QThread线程
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
LeetCode 0152. 乘积最大子数组:dp + 原地滚动
XAML WPF项目groupBox控件
OpenCV安装、QT、VS配置项目设置
Solve the problem that MySQL cannot insert Chinese data
explain 各字段介绍
暑假第二周总结博客
QPalette palette, frame color fill
el-form-item prop属性动态绑定不生效如何解决
随机推荐
【Day_12 0507】查找组成一个偶数最接近的两个素数
el-form-item prop属性动态绑定不生效如何解决
Topology零部件拆解3D可视化解决方案
WinRAR | Generate multiple installers into one installer
How opencv implements image skew correction
QT_事件类
顺序表的简单描述及代码的简单实现
Tower Defense Shoreline User Agreement
Map传值
B001 - 基于STM32的智能生态鱼缸
消息模板占位符的使用
MySQL关系型数据库事务的ACID特性与实现方法
opencv real-time face detection
SQL函数 TO_DATE(一)
QT_Event class
C language theory--a solid foundation for the written test and interview
QT基础功能,信号、槽
C#/VB.NET:从 PDF 文档中提取所有表格
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
【Day_10 0428】密码强度等级