当前位置:网站首页>【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 为字符集大小
边栏推荐
猜你喜欢

How to solve the dynamic binding of el-form-item prop attribute does not take effect

JVM运行时数据区与JMM内存模型是什么

【Day_12 0507】二进制插入

Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读

QT常用全局宏定义

QT_QThread线程

explain 各字段介绍

How to build a CMDB driven by consumption scenarios?
Detailed explanation of DBPack SQL Tracing function and data encryption function

Topology Parts Disassembly 3D Visualization Solution
随机推荐
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
WinRAR | 将多个安装程序生成一个安装程序
opencv syntax Mat type summary
QT commonly used global macro definitions
OpenCV installation, QT, VS configuration project settings
Clip-on multimeter use method, how to measure the voltage, current, resistance?
Leetcode74. Search 2D Matrix
What is the implementation principle of Go iota keyword and enumeration type
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
How to build a CMDB driven by consumption scenarios?
B002 - 基于嵌入式的老人定位追踪监测仪
AntDB数据库亮相24届高速展,助力智慧高速创新应用
The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
Zabbix6.0钉钉机器人告警
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--章节实验
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
QT_QThread线程
1065 A+B and C (64bit)
opencv如何实现图像倾斜校正