当前位置:网站首页>【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 build a CMDB driven by consumption scenarios?
- The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
- 行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
- 一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
- 粒子滤波 particle filter —从贝叶斯滤波到粒子滤波——Part-I(贝叶斯滤波)
- 打开微信客服
- 基于flowable的upp(统一流程平台)运行性能优化
- 请你说说多线程
- B001 - Intelligent ecological fish tank based on STM32
- odoo 编码规范(编程规范、编码指南)
猜你喜欢

打开微信客服

以消费场景为驱动的CMDB要怎么建?

QPalette调色板、框架色彩填充

Prometheus的Recording rules实践

Leetcode74. Search 2D Matrix

Leetcode75. 颜色分类
Detailed explanation of DBPack SQL Tracing function and data encryption function

How to build a CMDB driven by consumption scenarios?

B005 – 基于STC8的单片机智能路灯控制系统

B005 - STC8 based single chip microcomputer intelligent street light control system
随机推荐
B001 - Intelligent ecological fish tank based on STM32
COS 用户实践征文
Leetcode71. 简化路径
Review实战经典:2 种封装风格,你偏爱哪种?
opencv real-time face detection
QT commonly used global macro definitions
The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
WinRAR | 将多个安装程序生成一个安装程序
8月微软技术课程,欢迎参与
如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
Prometheus的Recording rules实践
AntDB数据库亮相24届高速展,助力智慧高速创新应用
QT_QDialog 对话框
加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
塔防海岸线用户协议
SQL函数 TO_DATE(一)
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
【无标题】setInterval和setTimeout详解
XAML WPF item groupBox control